/* * Copyright (c) 2025 Aldrik Ramaekers * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include "log.hpp" #include "ui.hpp" #include "locales.hpp" #include "strops.hpp" #include "administration_reader.hpp" #include "administration_writer.hpp" #include "tinyfiledialogs.h" #include "file_templates.hpp" mtx_t _save_file_mutex; static void on_administration_data_changed() { if (administration_writer_save_all_administration_info_blocking()) { ui_set_status(localize("status.saved")); } else { ui_set_status(localize("status.saveFailed")); } } static void on_administration_data_deleted(char id[MAX_LEN_ID]) { administration_writer_delete_entry(id); } static void on_invoice_changed(invoice* invoice) { administration_writer_save_invoice_blocking(*invoice); } static void on_contact_changed_changed(contact* contact) { administration_writer_save_contact_blocking(*contact); } static void on_taxrate_changed_changed(tax_rate* rate) { administration_writer_save_tax_rate_blocking(*rate); } static void on_costcenter_changed_changed(cost_center* cost_center) { administration_writer_save_cost_center_blocking(*cost_center); } static void on_project_changed_changed(project* project) { administration_writer_save_project_blocking(*project); } bool administration_writer_create() { administration_set_data_changed_event_callback(on_administration_data_changed); administration_set_data_deleted_event_callback(on_administration_data_deleted); administration_set_invoice_changed_event_callback(on_invoice_changed); administration_set_contact_changed_event_callback(on_contact_changed_changed); administration_set_taxrate_changed_event_callback(on_taxrate_changed_changed); administration_set_costcenter_changed_event_callback(on_costcenter_changed_changed); administration_set_project_changed_event_callback(on_project_changed_changed); return mtx_init(&_save_file_mutex, mtx_plain) == thrd_success; } 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*5; // 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) { 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); return result == 0; } static bool _administration_writer_delete_entry_by_name(char* entry) { STOPWATCH_START; bool result = 1; 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; zip_close(zip_write); if (result) log_info("Deleted entry '%s' in %.3fms.", entry, STOPWATCH_TIME); else log_error("Failed to delete entry '%s'.", entry); return result; } bool administration_writer_delete_entry(char* id) { char final_path[50]; snprintf(final_path, 50, "%s.xml", id); return _administration_writer_delete_entry_by_name(final_path); } static bool administration_writer_write_to_zip(char* entry_to_replace, char* orig_content, int final_length) { bool result = 1; 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_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; zip_entry_close(zip_write); zip_close(zip_write); return result; } ///////////////////////////// //// Invoices ///////////////////////////// static char* administration_writer_get_eas_id_for_contact(contact contact) { if (contact.type == contact_type::CONTACT_CONSUMER) { return "[CONSUMER]"; } // https://docs.peppol.eu/poacc/billing/3.0/codelist/eas/ char* country_code = contact.address.country_code; // Countries using tax identification numbers. if (strcmp(country_code, "AT") == 0) return contact.taxid; // Austria if (strcmp(country_code, "BE") == 0) return contact.taxid; // Belgium if (strcmp(country_code, "BG") == 0) return contact.taxid; // Bulgaria if (strcmp(country_code, "CY") == 0) return contact.taxid; // Cyprus if (strcmp(country_code, "CZ") == 0) return contact.taxid; // Czech Republic if (strcmp(country_code, "DE") == 0) return contact.taxid; // Germany if (strcmp(country_code, "EE") == 0) return contact.taxid; // Estonia if (strcmp(country_code, "FR") == 0) return contact.taxid; // France if (strcmp(country_code, "GR") == 0) return contact.taxid; // Greece if (strcmp(country_code, "HR") == 0) return contact.taxid; // Croatia if (strcmp(country_code, "HU") == 0) return contact.taxid; // Hungary if (strcmp(country_code, "IE") == 0) return contact.taxid; // Ireland if (strcmp(country_code, "LU") == 0) return contact.taxid; // Luxembourg if (strcmp(country_code, "LV") == 0) return contact.taxid; // Latvia if (strcmp(country_code, "MT") == 0) return contact.taxid; // Malta if (strcmp(country_code, "PL") == 0) return contact.taxid; // Poland if (strcmp(country_code, "PT") == 0) return contact.taxid; // Portugal if (strcmp(country_code, "RO") == 0) return contact.taxid; // Romania if (strcmp(country_code, "SI") == 0) return contact.taxid; // Slovenia if (strcmp(country_code, "SK") == 0) return contact.taxid; // Slovakia if (strcmp(country_code, "ES") == 0) return contact.taxid; // Spain // Countries using business identification numbers. if (strcmp(country_code, "NL") == 0) return contact.businessid; // Netherlands if (strcmp(country_code, "SE") == 0) return contact.businessid; // Sweden if (strcmp(country_code, "LT") == 0) return contact.businessid; // Lithuania if (strcmp(country_code, "IT") == 0) return contact.businessid; // Italy if (strcmp(country_code, "FI") == 0) return contact.businessid; // Finland if (strcmp(country_code, "DK") == 0) return contact.businessid; // Denmark return NULL; // Unknown country code } static char* administration_writer_get_eas_scheme_for_contact(contact contact) { if (contact.type == contact_type::CONTACT_CONSUMER) { return "0203"; // Hack } address addr = contact.address; // https://docs.peppol.eu/poacc/billing/3.0/codelist/eas/ char* country_code = addr.country_code; if (strcmp(country_code, "AT") == 0) return "9914"; // Austria if (strcmp(country_code, "BE") == 0) return "9925"; // Belgium if (strcmp(country_code, "BG") == 0) return "9926"; // Bulgaria if (strcmp(country_code, "CY") == 0) return "9928"; // Cyprus if (strcmp(country_code, "CZ") == 0) return "9929"; // Czech Republic if (strcmp(country_code, "DE") == 0) return "9930"; // Germany if (strcmp(country_code, "DK") == 0) return "0096"; // Denmark if (strcmp(country_code, "EE") == 0) return "9931"; // Estonia if (strcmp(country_code, "FR") == 0) return "9957"; // France if (strcmp(country_code, "GR") == 0) return "9933"; // Greece if (strcmp(country_code, "HR") == 0) return "9934"; // Croatia if (strcmp(country_code, "HU") == 0) return "9910"; // Hungary if (strcmp(country_code, "IE") == 0) return "9935"; // Ireland if (strcmp(country_code, "FI") == 0) return "0212"; // Finland if (strcmp(country_code, "IT") == 0) return "0208"; // Italy if (strcmp(country_code, "LT") == 0) return "0200"; // Lithuania if (strcmp(country_code, "LU") == 0) return "9938"; // Luxembourg if (strcmp(country_code, "LV") == 0) return "9939"; // Latvia if (strcmp(country_code, "MT") == 0) return "9943"; // Malta if (strcmp(country_code, "NL") == 0) return "0106"; // Netherlands if (strcmp(country_code, "PL") == 0) return "9945"; // Poland if (strcmp(country_code, "PT") == 0) return "9946"; // Portugal if (strcmp(country_code, "RO") == 0) return "9947"; // Romania if (strcmp(country_code, "SE") == 0) return "0007"; // Sweden if (strcmp(country_code, "SI") == 0) return "9949"; // Slovenia if (strcmp(country_code, "SK") == 0) return "9950"; // Slovakia if (strcmp(country_code, "ES") == 0) return "9920"; // Spain return NULL; // Unknown country code } bool isEmptyTag(const char *start, const char *end) { const char *ptr = start; while (ptr < end) { if (*ptr != ' ' && *ptr != '\n' && *ptr != '\t') { return false; } ptr++; } return true; } void _remove_empty_xml_tags(char* file_content, int depth) { for (int i = 0; i < depth; i++) { char *read = file_content; char *write = file_content; while (*read) { if (*read == '<') { char *tagStart = read; if (*(tagStart+1) != '/') { char *tagEnd = strchr(tagStart, '>'); if (!tagEnd) break; // malformed XML char *nextTagStart = strchr(tagEnd, '<'); if (*(nextTagStart+1) == '/') { // Check for empty tag char *closeTagStart = strstr(tagEnd + 1, "'); if (closeTagEnd) { read = closeTagEnd + 1; // skip entire empty tag continue; } } } } } *write++ = *read++; } *write = '\0'; // terminate the modified string } } bool administration_writer_save_invoice_blocking(invoice inv) { STOPWATCH_START; bool result = 1; int buf_length = 150000; // Ballpark file content size. char* file_content = (char*)malloc(buf_length); memset(file_content, 0, buf_length); memcpy(file_content, peppol_invoice_template, strlen(peppol_invoice_template)); struct tm *tm_info = 0; char date_buffer[11]; // "YYYY-MM-DD" + null terminator // properties not stored from supplier/customer/addressee: // - type // These can all be retrieved from existing contacts. // properties not stored from billing item: // - discount (can be recalculated from line_amount - (quantity * unit_price) ) // - tax (can be recalculated) // - total (can be recalculated) strops_replace(file_content, buf_length, "{{INVOICE_ID}}", inv.id); strops_replace(file_content, buf_length, "{{INVOICE_SEQUENCE_ID}}", inv.sequential_number); strops_replace(file_content, buf_length, "{{CURRENCY}}", inv.currency); strops_replace(file_content, buf_length, "{{PROJECT_ID}}", inv.project_id); strops_replace(file_content, buf_length, "{{COST_CENTER_ID}}", inv.cost_center_id); strops_replace(file_content, buf_length, "{{INVOICE_DOCUMENT}}", inv.document); strops_replace_int32(file_content, buf_length, "{{INVOICE_STATUS}}", (s32)inv.status); // Supplier data strops_replace(file_content, buf_length, "{{SUPPLIER_ENDPOINT_SCHEME}}", administration_writer_get_eas_scheme_for_contact(inv.supplier)); strops_replace(file_content, buf_length, "{{SUPPLIER_ENDPOINT_ID}}", administration_writer_get_eas_id_for_contact(inv.supplier)); strops_replace(file_content, buf_length, "{{SUPPLIER_ID}}", inv.supplier.id); strops_replace(file_content, buf_length, "{{SUPPLIER_NAME}}", inv.supplier.name); strops_replace(file_content, buf_length, "{{SUPPLIER_STREET}}", inv.supplier.address.address1); strops_replace(file_content, buf_length, "{{SUPPLIER_STREET2}}", inv.supplier.address.address2); strops_replace(file_content, buf_length, "{{SUPPLIER_CITY}}", inv.supplier.address.city); strops_replace(file_content, buf_length, "{{SUPPLIER_POSTAL}}", inv.supplier.address.postal); strops_replace(file_content, buf_length, "{{SUPPLIER_REGION}}", inv.supplier.address.region); strops_replace(file_content, buf_length, "{{SUPPLIER_COUNTRY}}", inv.supplier.address.country_code); strops_replace(file_content, buf_length, "{{SUPPLIER_VAT_ID}}", inv.supplier.taxid); strops_replace(file_content, buf_length, "{{SUPPLIER_LEGAL_NAME}}", inv.supplier.name); strops_replace(file_content, buf_length, "{{SUPPLIER_BUSINESS_ID}}", inv.supplier.businessid); strops_replace(file_content, buf_length, "{{SUPPLIER_PHONE_NUMBER}}", inv.supplier.phone_number); strops_replace(file_content, buf_length, "{{SUPPLIER_EMAIL}}", inv.supplier.email); // Customer data strops_replace(file_content, buf_length, "{{CUSTOMER_ENDPOINT_SCHEME}}", administration_writer_get_eas_scheme_for_contact(inv.customer)); strops_replace(file_content, buf_length, "{{CUSTOMER_ENDPOINT_ID}}", administration_writer_get_eas_id_for_contact(inv.customer)); strops_replace(file_content, buf_length, "{{CUSTOMER_ID}}", inv.customer.id); strops_replace(file_content, buf_length, "{{CUSTOMER_NAME}}", inv.customer.name); strops_replace(file_content, buf_length, "{{CUSTOMER_STREET}}", inv.customer.address.address1); strops_replace(file_content, buf_length, "{{CUSTOMER_STREET2}}", inv.customer.address.address2); strops_replace(file_content, buf_length, "{{CUSTOMER_CITY}}", inv.customer.address.city); strops_replace(file_content, buf_length, "{{CUSTOMER_POSTAL}}", inv.customer.address.postal); strops_replace(file_content, buf_length, "{{CUSTOMER_REGION}}", inv.customer.address.region); 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); strops_replace(file_content, buf_length, "{{CUSTOMER_LEGAL_NAME}}", inv.customer.name); strops_replace(file_content, buf_length, "{{CUSTOMER_BUSINESS_ID}}", inv.customer.businessid); strops_replace(file_content, buf_length, "{{CUSTOMER_PHONE_NUMBER}}", inv.customer.phone_number); strops_replace(file_content, buf_length, "{{CUSTOMER_EMAIL}}", inv.customer.email); // Delivery data tm_info = localtime(&inv.delivered_at); strftime(date_buffer, sizeof(date_buffer), "%Y-%m-%d", tm_info); strops_replace(file_content, buf_length, "{{DELIVERY_DATE}}", date_buffer); strops_replace(file_content, buf_length, "{{DELIVERY_NAME}}", inv.addressee.name); strops_replace(file_content, buf_length, "{{DELIVERY_STREET}}", inv.addressee.address.address1); strops_replace(file_content, buf_length, "{{DELIVERY_STREET2}}", inv.addressee.address.address2); strops_replace(file_content, buf_length, "{{DELIVERY_CITY}}", inv.addressee.address.city); strops_replace(file_content, buf_length, "{{DELIVERY_POSTAL}}", inv.addressee.address.postal); strops_replace(file_content, buf_length, "{{DELIVERY_REGION}}", inv.addressee.address.region); strops_replace(file_content, buf_length, "{{DELIVERY_COUNTRY}}", inv.addressee.address.country_code); // Payment means strops_replace_int32(file_content, buf_length, "{{PAYMENT_TYPE}}", inv.payment_means.payment_method); strops_replace(file_content, buf_length, "{{RECIPIENT_IBAN}}", inv.payment_means.payee_bank_account); strops_replace(file_content, buf_length, "{{RECIPIENT_NAME}}", inv.payment_means.payee_account_name); strops_replace(file_content, buf_length, "{{RECIPIENT_BIC}}", inv.payment_means.service_provider_id); strops_replace(file_content, buf_length, "{{SENDER_IBAN}}", inv.payment_means.payer_bank_account); // Tax breakdown strops_replace_float(file_content, buf_length, "{{TOTAL_TAX_AMOUNT}}", inv.tax, 2); { // Create tax subtotal list. tax_rate* tax_rate_buffer = (tax_rate*)malloc(sizeof(tax_rate)*administration_billing_item_count(&inv)); u32 tax_rate_count = administration_invoice_get_tax_rates(&inv, tax_rate_buffer); u32 tax_subtotal_list_buffer_size = (u32)strlen(peppol_invoice_tax_subtotal_template) * 2 * tax_rate_count; // Ballpark list size. char* tax_subtotal_list_buffer = (char*)malloc(tax_subtotal_list_buffer_size); memset(tax_subtotal_list_buffer, 0, tax_subtotal_list_buffer_size); u32 tax_subtotal_list_buffer_cursor = 0; for (u32 i = 0; i < tax_rate_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_rate(&inv, tax_rate_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}}", tax_rate_buffer[i].category_code); strops_replace_float(tax_entry_file_content, tax_entry_buf_length, "{{TAX_PERCENT}}", tax_rate_buffer[i].rate, 2); u32 content_len = (u32)strlen(tax_entry_file_content); memcpy(tax_subtotal_list_buffer+tax_subtotal_list_buffer_cursor, tax_entry_file_content, content_len); tax_subtotal_list_buffer_cursor += content_len; } tax_subtotal_list_buffer[tax_subtotal_list_buffer_cursor] = 0; strops_replace(file_content, buf_length, "{{TAX_SUBTOTAL_LIST}}", tax_subtotal_list_buffer); free(tax_subtotal_list_buffer); free(tax_rate_buffer); } // Totals strops_replace_float(file_content, buf_length, "{{LINE_EXTENSION_AMOUNT}}", inv.net - inv.allowance, 2); strops_replace_float(file_content, buf_length, "{{TAX_EXCLUSIVE_AMOUNT}}", inv.net, 2); strops_replace_float(file_content, buf_length, "{{TAX_INCLUSIVE_AMOUNT}}", inv.total, 2); strops_replace_float(file_content, buf_length, "{{PAYABLE_AMOUNT}}", inv.total, 2); // Invoice lines { billing_item* billing_item_buffer = (billing_item*)malloc(sizeof(billing_item) * administration_billing_item_count(&inv)); u32 billing_item_count = administration_billing_item_get_all_for_invoice(&inv, billing_item_buffer); u32 billing_item_list_buffer_size = (u32)strlen(peppol_invoice_line_template) * 2 * billing_item_count; // Ballpark list size. char* billing_item_list_buffer = (char*)malloc(billing_item_list_buffer_size); memset(billing_item_list_buffer, 0, billing_item_list_buffer_size); u32 billing_item_list_buffer_cursor = 0; for (u32 i = 0; i < billing_item_count; i++) { int billing_item_buf_length = 0; char* billing_item_file_content = administration_writer_copy_template(peppol_invoice_line_template, &billing_item_buf_length); billing_item bi = billing_item_buffer[i]; tax_rate rate; administration_tax_rate_get_by_id(&rate, bi.tax_rate_id); strops_replace(billing_item_file_content, billing_item_buf_length, "{{CURRENCY}}", bi.currency); strops_replace(billing_item_file_content, billing_item_buf_length, "{{LINE_ID}}", bi.id); strops_replace(billing_item_file_content, billing_item_buf_length, "{{LINE_TAX_ID}}", bi.tax_rate_id); strops_replace(billing_item_file_content, billing_item_buf_length, "{{ITEM_NAME}}", bi.description); strops_replace(billing_item_file_content, billing_item_buf_length, "{{LINE_TAX_CATEGORY}}", rate.category_code); strops_replace_float(billing_item_file_content, billing_item_buf_length, "{{LINE_TAX_PERCENT}}", rate.rate, 2); strops_replace_float(billing_item_file_content, billing_item_buf_length, "{{LINE_AMOUNT}}", bi.net, 2); // line amount = net_per_item * items_count - discount strops_replace_float(billing_item_file_content, billing_item_buf_length, "{{QUANTITY}}", bi.amount, 2); strops_replace_float(billing_item_file_content, billing_item_buf_length, "{{UNIT_PRICE}}", bi.net_per_item, 2); // unit price before discount strops_replace(billing_item_file_content, billing_item_buf_length, "{{UNIT_CODE}}", bi.amount_is_percentage ? "%" : "X"); if (bi.discount_is_percentage) { strops_replace_float(billing_item_file_content, billing_item_buf_length, "{{DISCOUNT_TOTAL_PERCENTAGE}}", bi.discount, 2); strops_replace_float(billing_item_file_content, billing_item_buf_length, "{{DISCOUNT_BASE_AMOUNT}}", bi.net + bi.allowance, 2); // Total net before discount. } else { strops_replace(billing_item_file_content, billing_item_buf_length, "{{DISCOUNT_TOTAL_PERCENTAGE}}", ""); strops_replace(billing_item_file_content, billing_item_buf_length, "{{DISCOUNT_BASE_AMOUNT}}", ""); } strops_replace_float(billing_item_file_content, billing_item_buf_length, "{{DISCOUNT_TOTAL}}", bi.allowance, 2); u32 content_len = (u32)strlen(billing_item_file_content); memcpy(billing_item_list_buffer+billing_item_list_buffer_cursor, billing_item_file_content, content_len); billing_item_list_buffer_cursor += content_len; } billing_item_list_buffer[billing_item_list_buffer_cursor] = 0; strops_replace(file_content, buf_length, "{{INVOICE_LINE_LIST}}", billing_item_list_buffer); free(billing_item_list_buffer); free(billing_item_buffer); } // Dates tm_info = localtime(&inv.issued_at); strftime(date_buffer, sizeof(date_buffer), "%Y-%m-%d", tm_info); strops_replace(file_content, buf_length, "{{ISSUE_DATE}}", date_buffer); tm_info = localtime(&inv.expires_at); strftime(date_buffer, sizeof(date_buffer), "%Y-%m-%d", tm_info); strops_replace(file_content, buf_length, "{{DUE_DATE}}", date_buffer); _remove_empty_xml_tags(file_content, 5); //// Write to Disk. char final_path[50]; snprintf(final_path, 50, "%s.xml", inv.id); 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); if (result) log_info("Saved invoice '%s' in %.3fms.", inv.sequential_number, STOPWATCH_TIME); else log_error("Failed to save invoice '%s'.", inv.sequential_number); return result; } static bool administration_writer_save_all_invoices_blocking() { bool result = 1; u32 num_invoices = administration_invoice_count(); u32 buffer_size = sizeof(invoice) * num_invoices; invoice* invoice_buffer = (invoice*)malloc(buffer_size); num_invoices = administration_invoice_get_all(invoice_buffer); for (u32 i = 0; i < num_invoices; i++) { invoice c = invoice_buffer[i]; if (!administration_writer_save_invoice_blocking(c)) result = 0; } free(invoice_buffer); return result; } ///////////////////////////// //// Projects ///////////////////////////// bool administration_writer_save_project_blocking(project project) { STOPWATCH_START; bool result = 1; int buf_length = 0; char* file_content = administration_writer_copy_template(project_save_template, &buf_length); struct tm *tm_info = 0; char date_buffer[11]; // "YYYY-MM-DD" + null terminator 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); tm_info = gmtime(&project.start_date); strftime(date_buffer, sizeof(date_buffer), "%Y-%m-%d", tm_info); strops_replace(file_content, buf_length, "{{PROJECT_STARTDATE}}", date_buffer); tm_info = gmtime(&project.end_date); strftime(date_buffer, sizeof(date_buffer), "%Y-%m-%d", tm_info); strops_replace(file_content, buf_length, "{{PROJECT_ENDDATE}}", date_buffer); //// Write to Disk. char final_path[50]; snprintf(final_path, 50, "%s.xml", project.id); 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); if (result) log_info("Saved project '%s' in %.3fms.", project.description, STOPWATCH_TIME); else log_error("Failed to save project '%s'.", project.description); return result; } static bool administration_writer_save_all_projects_blocking() { bool result = 1; u32 num_projects = administration_project_count(); u32 buffer_size = sizeof(project) * num_projects; project* project_buffer = (project*)malloc(buffer_size); num_projects = administration_project_get_all(project_buffer); for (u32 i = 0; i < num_projects; i++) { project c = project_buffer[i]; if (!administration_writer_save_project_blocking(c)) result = 0; } free(project_buffer); return result; } ///////////////////////////// //// Cost centers ///////////////////////////// bool administration_writer_save_cost_center_blocking(cost_center cost) { STOPWATCH_START; bool result = 1; int buf_length = 0; char* file_content = administration_writer_copy_template(costcenter_save_template, &buf_length); 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(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); if (result) log_info("Saved cost center '%s' in %.3fms.", cost.code, STOPWATCH_TIME); else log_error("Failed to save cost center '%s'.", cost.code); return result; } bool administration_writer_save_all_cost_centers_blocking() { bool result = 1; u32 num_costcenters = administration_cost_center_count(); u32 buffer_size = sizeof(cost_center) * num_costcenters; cost_center* costcenter_buffer = (cost_center*)malloc(buffer_size); num_costcenters = administration_cost_center_get_all(costcenter_buffer); for (u32 i = 0; i < num_costcenters; i++) { cost_center c = costcenter_buffer[i]; if (!administration_writer_save_cost_center_blocking(c)) result = 0; } free(costcenter_buffer); return result; } ///////////////////////////// //// Tax rates ///////////////////////////// bool administration_writer_save_tax_rate_blocking(tax_rate rate) { STOPWATCH_START; bool result = 1; int buf_length = 0; char* file_content = administration_writer_copy_template(taxrate_save_template, &buf_length); strops_replace(file_content, buf_length, "{{TAXBRACKET_ID}}", rate.id); strops_replace(file_content, buf_length, "{{TAXBRACKET_COUNTRY}}", rate.country_code); strops_replace_float(file_content, buf_length, "{{TAXBRACKET_RATE}}", rate.rate, 2); strops_replace(file_content, buf_length, "{{TAXBRACKET_CATEGORY}}", rate.category_code); //// Write to Disk. char final_path[50]; snprintf(final_path, 50, "%s.xml", rate.id); 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); if (result) log_info("Saved tax rate '%s/%.1f' in %.3fms.", rate.country_code, rate.rate, STOPWATCH_TIME); else log_error("Failed to save tax rate '%s/%.1f'.", rate.country_code, rate.rate); return result; } bool administration_writer_save_all_tax_rates_blocking() { //// Get all data. u32 num_rates = administration_tax_rate_count(); u32 buffer_size = sizeof(tax_rate) * num_rates; tax_rate* rate_buffer = (tax_rate*)malloc(buffer_size); num_rates = administration_tax_rate_get_all(rate_buffer); bool result = 1; for (u32 i = 0; i < num_rates; i++) { tax_rate c = rate_buffer[i]; if (!administration_writer_save_tax_rate_blocking(c)) result = 0; } free(rate_buffer); return result; } ///////////////////////////// //// Contacts ///////////////////////////// bool administration_writer_save_contact_blocking(contact c) { STOPWATCH_START; bool result = 1; 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(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); if (result) log_info("Saved contact '%s' in %.3fms.", c.name, STOPWATCH_TIME); else log_error("Failed to save contact '%s'.", c.name); return result; } static bool administration_writer_save_all_contacts_blocking() { //// Get all data. u32 num_contacts = administration_contact_count(); u32 buffer_size = sizeof(contact) * num_contacts; contact* contact_buffer = (contact*)malloc(buffer_size); num_contacts = administration_contact_get_all(contact_buffer); bool result = 1; // Save company info. if (!administration_writer_save_contact_blocking(administration_company_info_get())) result = 0; // Save contacts. for (u32 i = 0; i < num_contacts; i++) { contact c = ((contact*)contact_buffer)[i]; if (!administration_writer_save_contact_blocking(c)) result = 0; } free(contact_buffer); return result; } ///////////////////////////// //// Administration info ///////////////////////////// bool administration_writer_save_all_administration_info_blocking() { STOPWATCH_START; bool result = 1; int buf_length = 0; char* file_content = administration_writer_copy_template(administration_save_template, &buf_length); 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); 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(file_content); if (result) log_info("Saved administration info in %.3fms.", STOPWATCH_TIME); else log_error("Failed to save administration info."); return result; } ///////////////////////////// //// Other ///////////////////////////// static int administration_writer_write_all_t(void *arg) { (void)arg; int result = 1; mtx_lock(&_save_file_mutex); if (!administration_writer_save_all_invoices_blocking()) result = 0; if (!administration_writer_save_all_projects_blocking()) result = 0; if (!administration_writer_save_all_administration_info_blocking()) result = 0; if (!administration_writer_save_all_tax_rates_blocking()) result = 0; if (!administration_writer_save_all_cost_centers_blocking()) result = 0; if (!administration_writer_save_all_contacts_blocking()) result = 0; mtx_unlock(&_save_file_mutex); return result; } bool administration_writer_save_all_async() { administration_writer_write_all_t(0); //thrd_t thr; //if (thrd_create(&thr, administration_writer_write_all_t, 0) != thrd_success) { // return false; //} return true; }