summaryrefslogtreecommitdiff
path: root/src/administration_writer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/administration_writer.cpp')
-rw-r--r--src/administration_writer.cpp78
1 files changed, 67 insertions, 11 deletions
diff --git a/src/administration_writer.cpp b/src/administration_writer.cpp
index e00921d..0176d97 100644
--- a/src/administration_writer.cpp
+++ b/src/administration_writer.cpp
@@ -25,7 +25,7 @@ void administration_writer_destroy()
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.
+ 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);
@@ -206,18 +206,19 @@ bool administration_writer_save_invoice_blocking(invoice inv)
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);
+ 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);
-
+ 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);
+
+ u32 tax_subtotal_list_buffer_size = (u32)strlen(peppol_invoice_tax_subtotal_template) * 2 * tax_bracket_count; // Ballpark list size.
+ char* tax_subtotal_list_buffer = (char*)malloc(tax_subtotal_list_buffer_size);
+ u32 tax_subtotal_list_buffer_cursor = 0;
for (u32 i = 0; i < tax_bracket_count; i++)
{
@@ -230,15 +231,69 @@ bool administration_writer_save_invoice_blocking(invoice inv)
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(tax_entry_file_content, tax_entry_buf_length, "{{TAX_CATEGORY}}", tax_bracket_buffer[i].category_code);
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);
+ 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_bracket_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);
+ 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];
+ country_tax_bracket bracket;
+ administration_tax_bracket_get_by_id(bi.tax_bracket_id, &bracket);
+
+ strops_replace(billing_item_file_content, billing_item_buf_length, "{{CURRENCY}}", inv.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, "{{ITEM_NAME}}", "XD");
+ strops_replace(billing_item_file_content, billing_item_buf_length, "{{LINE_TAX_CATEGORY}}", bracket.category_code);
+ strops_replace_float(billing_item_file_content, billing_item_buf_length, "{{LINE_TAX_PERCENT}}", bracket.rate, 2);
+ strops_replace_float(billing_item_file_content, billing_item_buf_length, "{{LINE_AMOUNT}}", bi.net, 2);
+ 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);
+ strops_replace(billing_item_file_content, billing_item_buf_length, "{{UNIT_CODE}}", bi.amount_is_percentage ? "%" : "X");
+
+ 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);
@@ -249,6 +304,7 @@ bool administration_writer_save_invoice_blocking(invoice inv)
snprintf(final_path, 50, "%s.xml", inv.id);
int final_length = (int)strlen(file_content);
+ printf(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;
@@ -371,7 +427,7 @@ bool administration_writer_save_tax_bracket_blocking(country_tax_bracket bracket
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);
+ strops_replace(file_content, buf_length, "{{TAXBRACKET_CATEGORY}}", bracket.category_code);
//// Write to Disk.
char final_path[50];