summaryrefslogtreecommitdiff
path: root/src/administration_writer.cpp
diff options
context:
space:
mode:
authorAldrik Ramaekers <aldrikboy@gmail.com>2025-09-06 11:58:12 +0200
committerAldrik Ramaekers <aldrikboy@gmail.com>2025-09-06 11:58:12 +0200
commit68d0ef1586a90ddef6d0cef7ebd593828282c76a (patch)
tree6166037ca8a5d5ad168badc7ff5b76510bf05df4 /src/administration_writer.cpp
parentb970907ef53f1b8367285cbe7c2dc2a7fa47967f (diff)
working on invoice templates
Diffstat (limited to 'src/administration_writer.cpp')
-rw-r--r--src/administration_writer.cpp195
1 files changed, 106 insertions, 89 deletions
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;
}