/* * 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. */ #include #include #include #define CPPHTTPLIB_OPENSSL_SUPPORT #include "httplib.h" #include "log.hpp" #include "ai_service.hpp" #include "strops.hpp" #include "administration_reader.hpp" ai_provider_impl _ai_get_impl() { ai_provider provider = administration_get_ai_service().provider; switch(provider) { case AI_PROVIDER_OPENAI: return _chatgpt_api_provider; default: assert(0); break; } return ai_provider_impl {0}; } extern const char* peppol_invoice_template; extern const char* peppol_invoice_line_template; ai_request* ai_document_to_invoice(char* file_path) { ai_provider_impl impl = _ai_get_impl(); char file_id[100]; if (!impl.upload_file(file_path, file_id, 100)) { return 0; } size_t query_buffer_len = 50000; char* template_buffer = (char*)malloc(query_buffer_len); memset(template_buffer, 0, query_buffer_len); strncpy(template_buffer, peppol_invoice_template, query_buffer_len); strops_replace(template_buffer, 50000, "{{INVOICE_LINE_LIST}}", peppol_invoice_line_template); char* ai_query = "\n\nI have provided a file containing an invoice. Fill in the above Peppol 3.0 template with the information from the invoice." "Do not add any fields to the template. If you can't find data for a given field, leave it empty. Do not make up any information." "Only return the filled out template in valid XML format. Nothing else.\n"; size_t query_len = strlen(template_buffer); strncpy(template_buffer + query_len, ai_query, query_buffer_len - query_len); char* response; if (!impl.query_with_file(template_buffer, query_buffer_len, file_id, &response)) { return 0; } invoice inv; if (!administration_reader_read_invoice_from_xml(&inv, response, strlen(response))) { return false; } invoice tmp = administration_invoice_create_empty(); inv.status = invoice_status::INVOICE_RECEIVED; strops_copy(inv.id, tmp.id, MAX_LEN_ID); // TODO next_id is not being incremented strops_copy(inv.customer.id, MY_COMPANY_ID, MAX_LEN_ID); // TODO param for incomming/exporting necessary strops_copy(inv.document.original_path, file_path, MAX_LEN_PATH); strops_copy(inv.document.copy_path, "", MAX_LEN_PATH); a_err result = administration_invoice_import(&inv); free(template_buffer); free(response); return 0; }