From aa7b5ef6ab4f45d2e8e0caa7942db31fc60b3861 Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Sat, 27 Sep 2025 11:56:24 +0200 Subject: document selector for invoice --- src/administration_writer.cpp | 57 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 11 deletions(-) (limited to 'src/administration_writer.cpp') diff --git a/src/administration_writer.cpp b/src/administration_writer.cpp index b08723b..0cc4cd8 100644 --- a/src/administration_writer.cpp +++ b/src/administration_writer.cpp @@ -286,6 +286,49 @@ void _remove_empty_xml_tags(char* file_content, int depth) } } +static const char* _get_file_extension(const char *path) { + const char *dot = strrchr(path, '.'); + if (!dot || dot == path) return ""; + return dot; +} + +static void _add_document_to_zip(invoice* inv) +{ + document* doc = &inv->document; + + if (strlen(doc->copy_path) == 0 && strlen(doc->original_path) != 0) + { + char copy_path[MAX_LEN_PATH]; + snprintf(copy_path, MAX_LEN_PATH, "documents/%s%s", inv->sequential_number, _get_file_extension(doc->original_path)); + + FILE* orig_file = fopen(doc->original_path, "rb"); + if (orig_file == NULL) { + log_error("ERROR: original document file path does not exist."); + return; + } + + fseek(orig_file, 0L, SEEK_END); + long sz = ftell(orig_file); + fseek(orig_file, 0, SEEK_SET); + + char* file_copy = (char*)malloc(sz); + fread(file_copy, sz, 1, orig_file); + file_copy[sz-1] = 0; + + fclose(orig_file); + + if (administration_writer_write_to_zip(copy_path, file_copy, sz)) { + strops_copy(doc->copy_path, copy_path, MAX_LEN_PATH); + log_info("Made copy of '%s' to '%s'.", doc->original_path, doc->copy_path); + } + else { + log_error("ERROR: failed to make copy of original document '%s'.", doc->original_path); + } + + free(file_copy); + } +} + bool administration_writer_save_invoice_blocking(invoice inv) { STOPWATCH_START; @@ -299,21 +342,15 @@ bool administration_writer_save_invoice_blocking(invoice inv) 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) + _add_document_to_zip(&inv); 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(file_content, buf_length, "{{INVOICE_DOCUMENT_COPY}}", inv.document.copy_path); + strops_replace(file_content, buf_length, "{{INVOICE_DOCUMENT_ORIG}}", inv.document.original_path); strops_replace_int32(file_content, buf_length, "{{INVOICE_STATUS}}", (s32)inv.status); // Supplier data @@ -524,9 +561,7 @@ bool administration_writer_save_project_blocking(project project) 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); -- cgit v1.2.3-70-g09d2