#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include "ui.hpp" #include "strops.hpp" #include "administration_writer.hpp" #include "file_templates.hpp" mtx_t _save_file_mutex; bool administration_writer_create() { return mtx_init(&_save_file_mutex, mtx_plain) == thrd_success; } void administration_writer_destroy() { mtx_destroy(&_save_file_mutex); } static bool administration_writer_entry_exists(char* entry) { bool entry_exists = false; struct zip_t *zip_read = zip_open(administration_file_path_get(), 0, 'r'); int i, n = (int)zip_entries_total(zip_read); for (i = 0; i < n; ++i) { zip_entry_openbyindex(zip_read, i); const char *entry_name = zip_entry_name(zip_read); if (strcmp(entry_name, entry) == 0) { entry_exists = true; break; } zip_entry_close(zip_read); } zip_close(zip_read); return entry_exists; } bool administration_writer_delete_entry(char* id) { 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'); char final_path[50]; snprintf(final_path, 50, "%s.xml", id); char* indices[1] = {final_path}; if (zip_entries_delete(zip_write, indices, 1) < 0) result = 0; zip_close(zip_write); return result; } 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); 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'); if (entry_exists) { char* indices[1] = {entry_to_replace}; zip_entries_delete(zip_write, indices, 1); zip_close(zip_write); zip_write = zip_open(administration_file_path_get(), 0, 'a'); } 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) { // 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, "NL") == 0) return contact.taxid; // Netherlands 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, "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_address(address addr) { // 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 "9944"; // 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 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); 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 strops_replace(file_content, buf_length, "{{INVOICE_ID}}", inv.sequential_number); strops_replace(file_content, buf_length, "{{CURRENCY}}", inv.currency); // Supplier data strops_replace(file_content, buf_length, "{{SUPPLIER_ENDPOINT_SCHEME}}", administration_writer_get_eas_scheme_for_address(inv.supplier.address)); 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_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); // Customer data strops_replace(file_content, buf_length, "{{CUSTOMER_ENDPOINT_SCHEME}}", administration_writer_get_eas_scheme_for_address(inv.customer.address)); 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_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); // 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); //// 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); 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) { 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; file_content += sprintf(file_content, "\n"); file_content += sprintf(file_content, "\t%s\n", project.id); file_content += sprintf(file_content, "\t%s\n", project.description); file_content += sprintf(file_content, "\t%d\n", project.state); file_content += sprintf(file_content, "\t%lld\n", (long long)project.start_date); file_content += sprintf(file_content, "\t%lld\n", (long long)project.end_date); file_content += sprintf(file_content, "\n"); //// 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; free(orig_content); 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) { 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; file_content += sprintf(file_content, "\n"); file_content += sprintf(file_content, "\t%s\n", cost.id); file_content += sprintf(file_content, "\t%s\n", cost.code); file_content += sprintf(file_content, "\t%s\n", cost.description); file_content += sprintf(file_content, "\n"); //// 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); return result; } static 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 brackets ///////////////////////////// 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; file_content += sprintf(file_content, "\n"); file_content += sprintf(file_content, "\t%s\n", bracket.id); file_content += sprintf(file_content, "\t%s\n", bracket.country_code); file_content += sprintf(file_content, "\t%.2f\n", bracket.rate); file_content += sprintf(file_content, "\t%s\n", bracket.description); file_content += sprintf(file_content, "\n"); //// 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; free(orig_content); return result; } static bool administration_writer_save_all_tax_brackets_blocking() { //// Get all data. u32 num_brackets = administration_tax_bracket_count(); u32 buffer_size = sizeof(country_tax_bracket) * num_brackets; country_tax_bracket* bracket_buffer = (country_tax_bracket*)malloc(buffer_size); num_brackets = administration_tax_bracket_get_all(bracket_buffer); bool result = 1; for (u32 i = 0; i < num_brackets; i++) { country_tax_bracket c = bracket_buffer[i]; if (!administration_writer_save_tax_bracket_blocking(c)) result = 0; } free(bracket_buffer); return result; } ///////////////////////////// //// Contacts ///////////////////////////// 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, "\n"); file_content += sprintf(file_content, "\t%s\n", c.id); file_content += sprintf(file_content, "\t%s\n", c.name); file_content += sprintf(file_content, "\t%d\n", c.type); file_content += sprintf(file_content, "\t%s\n", c.taxid); file_content += sprintf(file_content, "\t%s\n", c.businessid); file_content += sprintf(file_content, "\t%s\n", c.email); file_content += sprintf(file_content, "\t%s\n", c.phone_number); file_content += sprintf(file_content, "\t%s\n", c.bank_account); file_content += sprintf(file_content, "\t
\n"); file_content += sprintf(file_content, "\t\t%s\n", c.address.address1); file_content += sprintf(file_content, "\t\t%s\n", c.address.address2); file_content += sprintf(file_content, "\t\t%s\n", c.address.country_code); file_content += sprintf(file_content, "\t
\n"); file_content += sprintf(file_content, "
\n"); 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; free(orig_content); 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() { 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; //// Cost centers. file_content += sprintf(file_content, "\n"); file_content += sprintf(file_content, "\t%d\n", administation_get()->next_id); file_content += sprintf(file_content, "\t%d\n", administation_get()->next_sequence_number); file_content += sprintf(file_content, "\t%s\n", administation_get()->program_version); file_content += sprintf(file_content, ""); //// 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; free(orig_content); 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_brackets_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); ui_set_status_loading(false); return result; } bool administration_writer_save_all_async() { ui_set_status_loading(true); thrd_t thr; if (thrd_create(&thr, administration_writer_write_all_t, 0) != thrd_success) { return false; } return true; }