} // End of the RunExport method #endregion #region Helper methods /// <summary> /// Get doxservr files /// </summary> /// <returns></returns> private async Task GetDoxservrFiles(string directory) { // Get new documents from doxservr DoxservrResponse<FileDocuments> dr = await this.dox_files_client.GetPage("", 0, -1, 0, 10); // Make sure that the model not is null if (dr.model == null) { // Log the error this.logger.LogError(dr.error); // Return from the method return; } // Loop as long as there is more posts to get while (true) { // Loop documents foreach (FileDocument fd in dr.model.items) { // Make sure that the file follows a standard if (string.Equals(fd.standard_name, "Annytab Dox Trade v1", StringComparison.OrdinalIgnoreCase) == false) { continue; } // File stream FileStream file_stream = null; try { // Create a file stream file_stream = System.IO.File.OpenWrite(directory + "\\Files\\" + fd.id + CommonTools.GetExtensions(fd.filename)); // Get the file DoxservrResponse<bool> file_response = await this.dox_files_client.GetFile(fd.id, file_stream); // Get the file if (file_response.model == false) { // Continue with the loop, the file was not downloaded continue; } // Save metadata to disk System.IO.File.WriteAllText(directory + "\\Files\\Meta\\" + fd.id + ".json", JsonConvert.SerializeObject(fd)); } catch (Exception ex) { // Log the error this.logger.LogError(ex, $"Save files to disk: {fd.id}", null); continue; } finally { // Dispose of the stream if (file_stream != null) { file_stream.Dispose(); } } } // Check if there is more files if (string.IsNullOrEmpty(dr.model.ct) == false) { // Get the next page dr = await this.dox_files_client.GetPage(dr.model.ct, 0, -1, 0, 10); // Make sure that the model not is null if(dr.model == null) { // Log the error this.logger.LogError(dr.error); // Break out from the loop break; } } else { // Break out from the loop break; } } } // End of the GetDoxservrFiles method
} // End of the constructor #endregion #region Update methods /// <summary> /// Update currency rates /// </summary> public async Task<DoxservrResponse<FixerRates>> UpdateCurrencyRates(string directory) { // Create the response to return DoxservrResponse<FixerRates> dr = new DoxservrResponse<FixerRates>(); // Set the file path string file_path = directory + "\\currency_rates.json"; try { // Get currency rates FixerRates file = JsonConvert.DeserializeObject<FixerRates>(System.IO.File.ReadAllText(file_path, Encoding.UTF8)); // Check if currency rates are up to date if (DateTime.Now.Date <= file.date.Date.AddDays(4)) { return dr; } } catch (System.IO.FileNotFoundException) { // File not found, it will be created } catch (Exception ex) { // Add error data dr.error = $"UpdateCurrencyRates: {ex.ToString()}"; } // Get the base currency string base_currency = string.IsNullOrEmpty(this.default_values.BaseCurrency) == false ? this.default_values.BaseCurrency : "SEK"; try { // Get the response HttpResponseMessage response = await this.client.GetAsync($"/latest?base={base_currency}"); // Get the data if (response.IsSuccessStatusCode == true) { // Get string data string data = await response.Content.ReadAsStringAsync(); // Save currency rates to a file System.IO.File.WriteAllText(file_path, data); // Get fixer rates dr.model = JsonConvert.DeserializeObject<FixerRates>(data); } else { // Get string data string data = await response.Content.ReadAsStringAsync(); // Add error data dr.error = $"UpdateCurrencyRates: {data}"; } } catch (Exception ex) { // Add exception data dr.error = $"UpdateCurrencyRates: {ex.ToString()}"; } // Return the response return dr; } // End of the UpdateCurrencyRates
} // End of the RunImport method /// <summary> /// Export from fortnox /// </summary> private async Task RunExport(string directory, IDoxservrFilesClient dox_files_client, IFortnoxClient nox_client) { // Log the start this.logger.LogInformation("START: Exporting documents from Fortnox!"); // Get offers OffersRoot offers_root = await this.fortnox_exporter.GetOffers(); // Make sure that offers not is null if(offers_root != null && offers_root.Offers != null) { // Log information this.logger.LogInformation($"Starts to process {offers_root.Offers.Count} offers!"); // Loop offers foreach (Offer post in offers_root.Offers) { // Get the document AnnytabDoxTradeRoot root = await this.fortnox_exporter.GetOffer(post.DocumentNumber); // Continue if the root is null if (root == null) { continue; } // Make sure that there is an email address if (string.IsNullOrEmpty(root.email) == true) { this.logger.LogError($"Offer: {root.document.id}, no email specified!"); continue; } // Variables string data = JsonConvert.SerializeObject(root.document); string filename = $"{root.document_type}_{root.document.id}.json"; string language_code = string.IsNullOrEmpty(root.language_code) == false ? root.language_code.ToLower() : "en"; DoxservrResponse<FileDocument> dr_file_metadata = null; try { // Send the document using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(data))) { dr_file_metadata = await this.dox_files_client.Send(stream, root.email, filename, "utf-8", "Annytab Dox Trade v1", language_code, "1"); } // Make sure that the file has been sent if (dr_file_metadata.model != null) { // Save the file System.IO.File.WriteAllText(directory + $"\\Files\\Exported\\{filename}", data, Encoding.UTF8); // Mark the offer as sent await this.nox_client.Action<OfferRoot>($"offers/{root.document.id}/externalprint"); // Log information this.logger.LogInformation($"Offer, {filename} has been sent to {root.email}!"); } } catch(Exception ex) { // Log the exception this.logger.LogError(ex, $"Offer: {root.document.id}", null); } } } // Get orders OrdersRoot orders_root = await this.fortnox_exporter.GetOrders(); // Make sure that orders not is null if(orders_root != null && orders_root.Orders != null) { // Log information this.logger.LogInformation($"Starts to process {orders_root.Orders.Count} orders!"); // Loop orders foreach (Order post in orders_root.Orders) { // Get documents (possibly 1 order and purchase orders) IList<AnnytabDoxTradeRoot> roots = await this.fortnox_exporter.GetOrder(post.DocumentNumber); // Continue if roots is null if (roots == null) { continue; } // Loop documents bool marked_as_sent = false; foreach (AnnytabDoxTradeRoot root in roots) { // Make sure that there is an email address if (string.IsNullOrEmpty(root.email) == true) { this.logger.LogError($"Order: {root.document.id}, no email specified!"); continue; } // Variables string data = JsonConvert.SerializeObject(root.document); string filename = $"{root.document_type}_{root.document.id}.json"; string language_code = string.IsNullOrEmpty(root.language_code) == false ? root.language_code.ToLower() : "en"; DoxservrResponse<FileDocument> dr_file_metadata = null; try { // Send the document using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(data))) { dr_file_metadata = await this.dox_files_client.Send(stream, root.email, filename, "utf-8", "Annytab Dox Trade v1", language_code, "1"); } // Make sure that the file has been sent if (dr_file_metadata.model != null) { // Save the file System.IO.File.WriteAllText(directory + $"\\Files\\Exported\\{filename}", data, Encoding.UTF8); // Mark the order as sent if (marked_as_sent == false) { await this.nox_client.Action<OrderRoot>($"orders/{root.document.id}/externalprint"); marked_as_sent = true; } // Log information this.logger.LogInformation($"Order, {filename} has been sent to {root.email}!"); } } catch (Exception ex) { // Log the exception this.logger.LogError(ex, $"Order: {root.document.id}", null); } } } } // Get invoices InvoicesRoot invoices_root = await this.fortnox_exporter.GetInvoices(); // Make sure that invoices not is null if(invoices_root != null && invoices_root.Invoices != null) { // Log information this.logger.LogInformation($"Starts to process {invoices_root.Invoices.Count} invoices!"); // Loop invoices foreach (Invoice post in invoices_root.Invoices) { // Get the document AnnytabDoxTradeRoot root = await this.fortnox_exporter.GetInvoice(post.DocumentNumber); // Continue if the root is null if (root == null) { continue; } // Make sure that there is an email address if (string.IsNullOrEmpty(root.email) == true) { this.logger.LogError($"Invoice: {root.document.id}, no email specified!"); continue; } // Variables string data = JsonConvert.SerializeObject(root.document); string filename = $"{root.document_type}_{root.document.id}.json"; string language_code = string.IsNullOrEmpty(root.language_code) == false ? root.language_code.ToLower() : "en"; DoxservrResponse<FileDocument> dr = null; try { // Send the document using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(data))) { dr = await this.dox_files_client.Send(stream, root.email, filename, "utf-8", "Annytab Dox Trade v1", language_code, "1"); } // Make sure that the file has been sent if (dr.model != null) { // Save the file System.IO.File.WriteAllText(directory + $"\\Files\\Exported\\{filename}", data, Encoding.UTF8); // Mark the invoice as sent await this.nox_client.Action<InvoiceRoot>($"invoices/{root.document.id}/externalprint"); // Log information this.logger.LogInformation($"Invoice, {filename} has been sent to {root.email}!"); } } catch (Exception ex) { // Log the exception this.logger.LogError(ex, $"Invoice: {root.document.id}", null); } } } // Log the end of the work this.logger.LogInformation("END: Exporting documents from Fortnox!"); } // End of the RunExport method