public async Task TestUploadFile() { // Set the file path string path = "D:\\Bilder\\1960.jpg"; using (FileStream stream = System.IO.File.OpenRead(path)) { FortnoxResponse<FileRoot> fr = await config.fortnox_client.UploadFile<FileRoot>(stream, "bil-test2.jpg", "inbox?path=inbox_v"); // Log the error if (fr.model == null) { config.logger.LogError(fr.error); } // Test evaluation Assert.AreNotEqual(null, fr.model); } } // End of the TestUploadFile method
public async Task TestUpdatePost() { // Create a post ContractAccrualRoot post = new ContractAccrualRoot { ContractAccrual = new ContractAccrual { AccrualAccount = "2990", CostAccount = "3600", Total = 50000M, AccrualRows = new List<AccrualRow> { new AccrualRow { Account = "2990", Credit = 0M, Debit = 5000M }, new AccrualRow { Account = "3600", Credit = 5000M, Debit = 0 } } } }; // Update the post FortnoxResponse<ContractAccrualRoot> fr = await config.fortnox_client.Update<ContractAccrualRoot>(post, "contractaccruals/1"); // Log the error if (fr.model == null) { config.logger.LogError(fr.error); } // Test evaluation Assert.AreNotEqual(null, fr.model); } // End of the TestUpdatePost method
public async Task TestUpdatePost() { // Create a post ContractRoot post = new ContractRoot { Contract = new Contract { DocumentNumber = "3", CustomerNumber = "10", ContractDate = "2016-04-01", PeriodStart = "2016-05-01", PeriodEnd = "2016-05-31", InvoiceRows = new List<InvoiceRow> { new InvoiceRow { ArticleNumber = "1", DeliveredQuantity = 5M }, new InvoiceRow { ArticleNumber = "2", DeliveredQuantity = 7M } } } }; // Update the post FortnoxResponse<ContractRoot> fr = await config.fortnox_client.Update<ContractRoot>(post, "contracts/3"); // Log the error if (fr.model == null) { config.logger.LogError(fr.error); } // Test evaluation Assert.AreNotEqual(null, fr.model); } // End of the TestUpdatePost method
public async Task TestAddPost() { // Create a post ContractTemplateRoot post = new ContractTemplateRoot { ContractTemplate = new ContractTemplate { TemplateNumber = "2", TemplateName = "6 Månader", ContractLength = 6, Continuous = false, InvoiceInterval = 3, InvoiceRows = new List<InvoiceRow> { new InvoiceRow { ArticleNumber = "1", DeliveredQuantity = 5M }, new InvoiceRow { ArticleNumber = "2", DeliveredQuantity = 3M } } } }; // Add the post FortnoxResponse<ContractTemplateRoot> fr = await config.fortnox_client.Add<ContractTemplateRoot>(post, "contracttemplates"); // Log the error if (fr.model == null) { config.logger.LogError(fr.error); } // Test evaluation Assert.AreNotEqual(null, fr.model); } // End of the TestAddPost method
public async Task TestUpdatePost() { // Create a post OrderRoot post = new OrderRoot { Order = new Order { DocumentNumber = "100001", CustomerNumber = "10", OrderRows = new List<OrderRow> { new OrderRow { ArticleNumber = "1", OrderedQuantity = 20M, DeliveredQuantity = 15M, Unit = "h" }, new OrderRow { ArticleNumber = "3", OrderedQuantity = 10M, DeliveredQuantity = 4M, } } } }; // Update the post FortnoxResponse<OrderRoot> fr = await config.fortnox_client.Update<OrderRoot>(post, "orders/100001"); // Log the error if (fr.model == null) { config.logger.LogError(fr.error); } // Test evaluation Assert.AreNotEqual(null, fr.model); } // End of the TestUpdatePost method
public async Task TestUpdatePost() { // Create a post OfferRoot post = new OfferRoot { Offer = new Offer { CustomerNumber = "11", OfferRows = new List<OfferRow> { new OfferRow { ArticleNumber = "3", Quantity = 50M, Price = 34.45M }, new OfferRow { ArticleNumber = "2", Quantity = 4M } } } }; // Update the post FortnoxResponse<OfferRoot> fr = await config.fortnox_client.Update<OfferRoot>(post, "offers/19"); // Log the error if (fr.model == null) { config.logger.LogError(fr.error); } // Test evaluation Assert.AreNotEqual(null, fr.model); } // End of the TestUpdatePost method
} // End of the AddCurrency method /// <summary> /// Add a unit if it does not exists /// </summary> public async Task<UnitRoot> AddUnit(string unit_code) { // Get the root FortnoxResponse<UnitRoot> fr = await this.nox_client.Get<UnitRoot>($"units/{unit_code}"); // Log errors if (string.IsNullOrEmpty(fr.error) == false) { this.logger.LogError(fr.error); } // Add the unit if it does not exist if (fr.model == null) { // Create a new post fr.model = new UnitRoot { Unit = new Unit { Code = unit_code, Description = unit_code } }; // Add a unit fr = await this.nox_client.Add<UnitRoot>(fr.model, $"units"); // Log errors if (string.IsNullOrEmpty(fr.error) == false) { this.logger.LogError(fr.error); } } // Return the post return fr.model; } // End of the AddUnit method
} // End of the AddPriceList method /// <summary> /// Add an account if it does not exist /// </summary> public async Task<AccountRoot> AddAccount(string account_number) { // Get the root FortnoxResponse<AccountRoot> fr = await this.nox_client.Get<AccountRoot>($"accounts/{account_number}"); // Log errors if (string.IsNullOrEmpty(fr.error) == false) { this.logger.LogError(fr.error); } // Add the account if it does not exist if (fr.model == null) { // Create a new post fr.model = new AccountRoot { Account = new Account { Number = account_number, Description = account_number } }; // Add an account fr = await this.nox_client.Add<AccountRoot>(fr.model, $"accounts"); // Log errors if (string.IsNullOrEmpty(fr.error) == false) { this.logger.LogError(fr.error); } } // Return the post return fr.model; } // End of the AddAccount method
} // End of the AddUnit method /// <summary> /// Add a price list if it does not exists /// </summary> public async Task<PriceListRoot> AddPriceList(string code) { // Get the root FortnoxResponse<PriceListRoot> fr = await this.nox_client.Get<PriceListRoot>($"pricelists/{code}"); // Log errors if (string.IsNullOrEmpty(fr.error) == false) { this.logger.LogError(fr.error); } // Add the price list if it does not exist if (fr.model == null) { // Create a new post fr.model = new PriceListRoot { PriceList = new PriceList { Code = code, Description = code } }; // Add a price list fr = await this.nox_client.Add<PriceListRoot>(fr.model, $"pricelists"); // Log errors if (string.IsNullOrEmpty(fr.error) == false) { this.logger.LogError(fr.error); } } // Return the post return fr.model; } // End of the AddPriceList method
public async Task TestAddPost() { // Create a post InvoiceRoot post = new InvoiceRoot { Invoice = new Invoice { CustomerNumber = "TEST-FREDDE", InvoiceRows = new List<InvoiceRow> { new InvoiceRow { ArticleNumber = "1", DeliveredQuantity = 8M }, new InvoiceRow { ArticleNumber = "2", DeliveredQuantity = 4M } } } }; // Add the post FortnoxResponse<InvoiceRoot> fr = await config.fortnox_client.Add<InvoiceRoot>(post, "invoices"); // Log the error if (fr.model == null) { config.logger.LogError(fr.error); } // Test evaluation Assert.AreNotEqual(null, fr.model); } // End of the TestAddPost method
public async Task TestUpdatePost() { // Create a post AbsenceTransactionRoot post = new AbsenceTransactionRoot { AbsenceTransaction = new AbsenceTransaction { Hours = 6M } }; // Update the post FortnoxResponse<AbsenceTransactionRoot> fr = await config.fortnox_client.Update<AbsenceTransactionRoot>(post, "absencetransactions/1/2018-09-10/SJK"); // Log the error if (fr.model == null) { config.logger.LogError(fr.error); } // Test evaluation Assert.AreNotEqual(null, fr.model); } // End of the TestUpdatePost method
} // End of the GetLabels method /// <summary> /// Get company settings /// </summary> public async Task<CompanySettingsRoot> GetCompanySettings() { // Return company settings if they already exists if(this._company_settings != null) { return this._company_settings; } // Get company settings FortnoxResponse<CompanySettingsRoot> fr = await this.nox_client.Get<CompanySettingsRoot>("settings/company"); // Log errors if (string.IsNullOrEmpty(fr.error) == false) { this.logger.LogError(fr.error); } // Create a reference to company settings this._company_settings = fr.model; // Return company settings return this._company_settings; } // End of the GetCompanySettings method
public async Task TestUpdatePost() { // Create a post ScheduleTimeRoot post = new ScheduleTimeRoot { ScheduleTime = new ScheduleTime { Hours = 12M } }; // Update the post FortnoxResponse<ScheduleTimeRoot> fr = await config.fortnox_client.Update<ScheduleTimeRoot>(post, "scheduletimes/2/2018-01-06"); // Log the error if (fr.model == null) { config.logger.LogError(fr.error); } // Test evaluation Assert.AreNotEqual(null, fr.model); } // End of the TestUpdatePost method
public async Task TestAddPost() { // Create a post TrustedDomainRoot post = new TrustedDomainRoot { TrustedDomain = new TrustedDomain { Domain = "doxservr.com" } }; // Add the post FortnoxResponse<TrustedDomainRoot> fr = await config.fortnox_client.Add<TrustedDomainRoot>(post, "emailtrusteddomains"); // Log the error if (fr.model == null) { config.logger.LogError(fr.error); } // Test evaluation Assert.AreNotEqual(null, fr.model); } // End of the TestAddPost method
public async Task TestAddPost() { // Create a post FolderRoot post = new FolderRoot { Folder = new Folder { Name = "sub-folder" } }; // Add the post FortnoxResponse<FolderRoot> fr = await config.fortnox_client.Add<FolderRoot>(post, "archive/?path=test\\sub"); // Log the error if (fr.model == null) { config.logger.LogError(fr.error); } // Test evaluation Assert.AreNotEqual(null, fr.model); } // End of the TestAddPost method
public async Task TestUpdatePost() { // Create a post PreDefinedAccountRoot post = new PreDefinedAccountRoot { PreDefinedAccount = new PreDefinedAccount { Account = "1930" } }; // Update the post FortnoxResponse<PreDefinedAccountRoot> fr = await config.fortnox_client.Update<PreDefinedAccountRoot>(post, "predefinedaccounts/BG"); // Log the error if (fr.model == null) { config.logger.LogError(fr.error); } // Test evaluation Assert.AreNotEqual(null, fr.model); } // End of the TestUpdatePost method
public async Task TestUpdatePost() { // Create a post LabelRoot post = new LabelRoot { Label = new Label { Description = "TEST-UPDATED" } }; // Update the post FortnoxResponse<LabelRoot> fr = await config.fortnox_client.Update<LabelRoot>(post, "labels/6"); // Log the error if (fr.model == null) { config.logger.LogError(fr.error); } // Test evaluation Assert.AreNotEqual(null, fr.model); } // End of the TestUpdatePost method
public async Task TestAddPost() { // Create a post TrustedSenderRoot post = new TrustedSenderRoot { TrustedSender = new EmailSender { Email = "*****@*****.**" } }; // Add the post FortnoxResponse<TrustedSenderRoot> fr = await config.fortnox_client.Add<TrustedSenderRoot>(post, "emailsenders/trusted"); // Log the error if (fr.model == null) { config.logger.LogError(fr.error); } // Test evaluation Assert.AreNotEqual(null, fr.model); } // End of the TestAddPost method
} // End of the constructor #endregion #region Get methods /// <summary> /// Get labels /// </summary> public async Task<Dictionary<string, string>> GetLabels() { // Return labels if they already exists if (this._labels != null) { return this._labels; } // Get labels FortnoxResponse<LabelsRoot> fr = await this.nox_client.Get<LabelsRoot>("labels"); // Log errors if (string.IsNullOrEmpty(fr.error) == false) { this.logger.LogError(fr.error); } // Make sure that root and root.Labels not is null if (fr.model == null || fr.model.Labels == null) { return new Dictionary<string, string>(); } // Create a dictionary this._labels = new Dictionary<string, string>(); // Loop the list foreach(Label label in fr.model.Labels) { this._labels.Add(label.Id, label.Description); } // Return a reference to labels return this._labels; } // End of the GetLabels method
public async Task TestUpdatePost() { // Create a post SupplierInvoiceRoot post = new SupplierInvoiceRoot { SupplierInvoice = new SupplierInvoice { SupplierNumber = "1", InvoiceDate = "2017-11-01", DueDate = "2017-11-25", Total = 10000, VAT = 2000, VATType = "NORMAL", //SalesType = "STOCK", // Ett fel uppstod i lagermodulen (2003127) Currency = "SEK", CurrencyRate = 1, CurrencyUnit = 1, SupplierInvoiceRows = new List<SupplierInvoiceRow> { new SupplierInvoiceRow { Account = "2440", Code = "TOT", AccountDescription = "Leverantörsskulder", Debit = 0, Credit = 10000, Total = -10000 }, new SupplierInvoiceRow { Account = "2641", Code = "VAT", AccountDescription = "Ingående moms", Debit = 2000, Credit = 0, Total = 2000 }, new SupplierInvoiceRow { Account = "6250", Code = "PRE", AccountDescription = "Porto", Debit = 8000, Credit = 0, Total = 8000, ArticleNumber = "1", Quantity = 1000, Price = 8 } } } }; // Update the post FortnoxResponse<SupplierInvoiceRoot> fr = await config.fortnox_client.Update<SupplierInvoiceRoot>(post, "supplierinvoices/50"); // Log the error if (fr.model == null) { config.logger.LogError(fr.error); } // Test evaluation Assert.AreNotEqual(null, fr.model); } // End of the TestUpdatePost method
} // End of the GetOrders method /// <summary> /// Get an order to export /// </summary> public async Task<IList<AnnytabDoxTradeRoot>> GetOrder(string id) { // Get data Dictionary<string, string> labels = await GetLabels(); CompanySettingsRoot company = await GetCompanySettings(); // Make sure that company and company.Settings not is null if (company == null || company.CompanySettings == null) { this.logger.LogError($"GetOrder: {id}, Could not find any company settings."); return null; } // Get the order FortnoxResponse<OrderRoot> fr = await this.nox_client.Get<OrderRoot>($"orders/{id}"); // Return if model or model.Order is null if (fr.model == null || fr.model.Order == null) { // Log the error and return null this.logger.LogError(fr.error); return null; } // Check if the order should be exported bool export_order = false; bool export_purchase_orders = false; foreach (Label label in fr.model.Order.Labels) { if (labels.ContainsKey(label.Id) && labels[label.Id] == "a-dox-trade-v1") { // Export the order export_order = true; } else if (labels.ContainsKey(label.Id) && labels[label.Id] == "a-dox-trade-v1-po") { // Export the purchase order export_purchase_orders = true; } } // Return null if nothing should be exported if (export_order == false && export_purchase_orders == false) { return null; } // Get the customer FortnoxResponse<CustomerRoot> fr_customer = await this.nox_client.Get<CustomerRoot>($"customers/{fr.model.Order.CustomerNumber}"); // Return if model or model.Customer is null if (fr_customer.model == null || fr_customer.model.Customer == null) { // Log the error and return null this.logger.LogError(fr_customer.error); return null; } // Create the list to return IList<AnnytabDoxTradeRoot> posts = new List<AnnytabDoxTradeRoot>(); // Create an order confirmation if(export_order == true) { AnnytabDoxTrade post = await CreateOrderConfirmation(company, fr.model, fr_customer.model); posts.Add(new AnnytabDoxTradeRoot { document_type = post.document_type, document = post, email = fr_customer.model.Customer.Email, language_code = fr.model.Order.Language }); } // Create purchase orders if(export_purchase_orders == true) { // Create variables IDictionary<string, SupplierRoot> suppliers = new Dictionary<string, SupplierRoot>(); IDictionary<string, IList<ProductRow>> supplier_rows = new Dictionary<string, IList<ProductRow>>(); decimal? total_weight = 0M; // Get suppliers foreach (OrderRow row in fr.model.Order.OrderRows) { // Get the article FortnoxResponse<ArticleRoot> fr_article = await this.nox_client.Get<ArticleRoot>($"articles/{row.ArticleNumber}"); // Make sure that the article was found if (fr_article.model != null && fr_article.model.Article != null) { // Get the supplier if(string.IsNullOrEmpty(fr_article.model.Article.SupplierNumber) == false) { // Check if the supplier exists if (suppliers.ContainsKey(fr_article.model.Article.SupplierNumber) == false) { // Get the supplier FortnoxResponse<SupplierRoot> fr_supplier = await this.nox_client.Get<SupplierRoot>($"suppliers/{fr_article.model.Article.SupplierNumber}"); // Add the supplier if(fr_supplier != null && fr_supplier.model != null) { // Add the supplier suppliers.Add(fr_article.model.Article.SupplierNumber, fr_supplier.model); } else { this.logger.LogError(fr_supplier.error); } } // Check if the supplier has order rows if(supplier_rows.ContainsKey(fr_article.model.Article.SupplierNumber) == false && suppliers.ContainsKey(fr_article.model.Article.SupplierNumber) == true) { // Add the row supplier_rows.Add(fr_article.model.Article.SupplierNumber, new List<ProductRow>()); } // Add to the total weight total_weight += fr_article.model.Article.Weight != null ? (fr_article.model.Article.Weight * row.OrderedQuantity) / 1000M : 0; // Add the row supplier_rows[fr_article.model.Article.SupplierNumber].Add(new ProductRow { product_code = fr_article.model.Article.ArticleNumber, manufacturer_code = fr_article.model.Article.ManufacturerArticleNumber, gtin = fr_article.model.Article.EAN, product_name = fr_article.model.Article.Description, vat_rate = row.VAT / 100, quantity = row.OrderedQuantity, unit_code = row.Unit, unit_price = fr_article.model.Article.PurchasePrice, // 0M subrows = null }); } } else { // Log the error this.logger.LogError(fr_article.error); } } // Create a purchase order to each supplier foreach(KeyValuePair<string, SupplierRoot> entry in suppliers) { // Create a purchase order AnnytabDoxTrade post = CreatePurchaseOrder(company, fr.model, entry.Value, supplier_rows[entry.Key], total_weight); // Add the document posts.Add(new AnnytabDoxTradeRoot { document_type = "purchase_order_" + entry.Value.Supplier.SupplierNumber, document = post, email = entry.Value.Supplier.Email, language_code = "en" }); } } // Return the list with posts return posts; } // End of the GetOrder method
} // End of the AddOffer method /// <summary> /// Add an order /// </summary> public async Task<OrderRoot> AddOrder(string dox_email, AnnytabDoxTrade doc) { // Terms of delivery if (string.IsNullOrEmpty(doc.terms_of_delivery) == false) { doc.terms_of_delivery = CommonTools.ConvertToAlphanumeric(doc.terms_of_delivery).ToUpper(); await AddTermsOfDelivery(doc.terms_of_delivery); } // Terms of payment if (string.IsNullOrEmpty(doc.terms_of_payment) == false) { doc.terms_of_payment = CommonTools.ConvertToAlphanumeric(doc.terms_of_payment).ToUpper().Replace("-", ""); await AddTermsOfPayment(doc.terms_of_payment); } // Way of delivery if (string.IsNullOrEmpty(doc.mode_of_delivery) == false) { doc.mode_of_delivery = CommonTools.ConvertToAlphanumeric(doc.mode_of_delivery).ToUpper(); await AddWayOfDelivery(doc.mode_of_delivery); } // Currency if (string.IsNullOrEmpty(doc.currency_code) == false) { doc.currency_code = doc.currency_code.ToUpper(); await AddCurrency(doc.currency_code); } // Upsert the customer CustomerRoot customer_root = await UpsertCustomer(dox_email, doc); // Return if the customer is null if (customer_root == null || customer_root.Customer == null) { return null; } // Create a list with order rows IList<OrderRow> rows = new List<OrderRow>(); // Add order rows if (doc.product_rows != null) { await AddOrderRows(doc.product_rows, rows); } // Create an order OrderRoot root = new OrderRoot { Order = new Order { CustomerNumber = customer_root.Customer.CustomerNumber, OrderDate = string.IsNullOrEmpty(doc.issue_date) == false ? doc.issue_date : null, DeliveryDate = string.IsNullOrEmpty(doc.delivery_date) == false ? doc.delivery_date : null, YourOrderNumber = doc.buyer_references != null && doc.buyer_references.ContainsKey("order_id") ? doc.buyer_references["order_id"] : null, ExternalInvoiceReference1 = string.IsNullOrEmpty(doc.payment_reference) == false ? doc.payment_reference : null, ExternalInvoiceReference2 = string.IsNullOrEmpty(doc.id) == false ? doc.id : null, Comments = doc.comment, OrderRows = rows, Currency = doc.currency_code, VATIncluded = false } }; // Add the order FortnoxResponse<OrderRoot> fr = await this.nox_client.Add<OrderRoot>(root, "orders"); // Log errors if (string.IsNullOrEmpty(fr.error) == false) { this.logger.LogError(fr.error); } // Return the order return fr.model; } // End of the AddOrder method
} // End of the AddOrder method /// <summary> /// Add an supplier invoice /// </summary> public async Task<SupplierInvoiceRoot> AddSupplierInvoice(string dox_email, AnnytabDoxTrade doc) { // Terms of payment if (string.IsNullOrEmpty(doc.terms_of_payment) == false) { doc.terms_of_payment = CommonTools.ConvertToAlphanumeric(doc.terms_of_payment).ToUpper().Replace("-", ""); await AddTermsOfPayment(doc.terms_of_payment); } // Currency if (string.IsNullOrEmpty(doc.currency_code) == false) { doc.currency_code = doc.currency_code.ToUpper(); await AddCurrency(doc.currency_code); } // Upsert the supplier SupplierRoot supplier_root = await UpsertSupplier(dox_email, doc); // Return if the supplier_root is null if(supplier_root == null || supplier_root.Supplier == null) { return null; } // Create a list with supplier invoice rows IList<SupplierInvoiceRow> rows = new List<SupplierInvoiceRow>(); //// Add accounts payable amount //if(doc.total != null && doc.total != 0M) //{ // rows.Add(new SupplierInvoiceRow // { // Code = "TOT", // Total = doc.total * -1 // }); //} //// Add value added tax //if (doc.vat_total != null && doc.vat_total != 0M) //{ // rows.Add(new SupplierInvoiceRow // { // Code = "VAT", // Total = doc.vat_total // }); //} //// Add rounding //if(doc.rounding != null && doc.rounding != 0M) //{ // rows.Add(new SupplierInvoiceRow // { // Code = "ROV", // Total = doc.rounding // }); //} // Add supplier invoice rows if (doc.product_rows != null) { await AddSupplierInvoiceRows(doc.product_rows, rows); } // Create a supplier invoice SupplierInvoiceRoot root = new SupplierInvoiceRoot { SupplierInvoice = new SupplierInvoice { SupplierNumber = supplier_root.Supplier.SupplierNumber, InvoiceNumber = string.IsNullOrEmpty(doc.payment_reference) == false ? doc.payment_reference : null, InvoiceDate = string.IsNullOrEmpty(doc.issue_date) == false ? doc.issue_date : null, DueDate = string.IsNullOrEmpty(doc.due_date) == false ? doc.due_date : null, Currency = doc.currency_code, Comments = doc.comment, Total = doc.total != null ? doc.total : 0M, VAT = doc.vat_total != null ? doc.vat_total : 0M, RoundOffValue = doc.rounding != null ? doc.rounding : 0M, SupplierInvoiceRows = rows } }; // Add a supplier invoice FortnoxResponse<SupplierInvoiceRoot> fr = await this.nox_client.Add<SupplierInvoiceRoot>(root, "supplierinvoices"); // Log errors if (string.IsNullOrEmpty(fr.error) == false) { this.logger.LogError(fr.error); } // Return the supplier invoice return fr.model; } // End of the AddSupplierInvoice method
} // End of the UpsertCustomer method /// <summary> /// Add or update a supplier /// </summary> public async Task<SupplierRoot> UpsertSupplier(string dox_email, AnnytabDoxTrade doc) { // Create variables FortnoxResponse<SupplierRoot> fr = new FortnoxResponse<SupplierRoot>(); bool supplier_exists = false; string supplier_email = doc.seller_information != null && string.IsNullOrEmpty(doc.seller_information.email) == false ? doc.seller_information.email : dox_email; // Find suppliers on email FortnoxResponse<SuppliersRoot> fr_page = await this.nox_client.Get<SuppliersRoot>($"suppliers?email={supplier_email}"); // Log errors if (string.IsNullOrEmpty(fr_page.error) == false) { this.logger.LogError(fr_page.error); } // Make sure that at least one supplier was found if (fr_page.model != null && fr_page.model.Suppliers != null && fr_page.model.Suppliers.Count > 0) { // Get a supplier fr = await this.nox_client.Get<SupplierRoot>($"suppliers/{fr_page.model.Suppliers[0].SupplierNumber}"); // Log errors if (string.IsNullOrEmpty(fr.error) == false) { this.logger.LogError(fr.error); } } // Check if the supplier exists if (fr.model != null) { supplier_exists = true; } else { fr.model = new SupplierRoot { Supplier = new Supplier() }; } // Update the supplier fr.model.Supplier.Email = supplier_email; if (doc.buyer_information != null) { fr.model.Supplier.OurReference = string.IsNullOrEmpty(fr.model.Supplier.OurReference) == true ? doc.buyer_information.contact_name : fr.model.Supplier.OurReference; } if(doc.seller_information != null) { fr.model.Supplier.Name = string.IsNullOrEmpty(doc.seller_information.person_name) == false ? doc.seller_information.person_name : fr.model.Supplier.Name; fr.model.Supplier.OrganisationNumber = string.IsNullOrEmpty(doc.seller_information.person_id) == false ? doc.seller_information.person_id : fr.model.Supplier.OrganisationNumber; fr.model.Supplier.VATNumber = string.IsNullOrEmpty(doc.seller_information.vat_number) == false ? doc.seller_information.vat_number : fr.model.Supplier.VATNumber; fr.model.Supplier.YourReference = string.IsNullOrEmpty(doc.seller_information.contact_name) == false ? doc.seller_information.contact_name : fr.model.Supplier.YourReference; fr.model.Supplier.Phone1 = string.IsNullOrEmpty(doc.seller_information.phone_number) == false ? doc.seller_information.phone_number : fr.model.Supplier.Phone1; fr.model.Supplier.Address1 = string.IsNullOrEmpty(doc.seller_information.address_line_1) == false ? doc.seller_information.address_line_1 : fr.model.Supplier.Address1; fr.model.Supplier.Address2 = string.IsNullOrEmpty(doc.seller_information.address_line_2) == false ? doc.seller_information.address_line_2 : fr.model.Supplier.Address2; fr.model.Supplier.ZipCode = string.IsNullOrEmpty(doc.seller_information.postcode) == false ? doc.seller_information.postcode : fr.model.Supplier.ZipCode; fr.model.Supplier.City = string.IsNullOrEmpty(doc.seller_information.city_name) == false ? doc.seller_information.city_name : fr.model.Supplier.City; fr.model.Supplier.CountryCode = string.IsNullOrEmpty(doc.seller_information.country_code) == false ? doc.seller_information.country_code : fr.model.Supplier.CountryCode; } fr.model.Supplier.Currency = string.IsNullOrEmpty(doc.currency_code) == false ? doc.currency_code : fr.model.Supplier.Currency; fr.model.Supplier.TermsOfPayment = string.IsNullOrEmpty(doc.terms_of_payment) == false ? doc.terms_of_payment : fr.model.Supplier.TermsOfPayment; fr.model.Supplier.VATType = string.IsNullOrEmpty(fr.model.Supplier.VATType) == true ? "NORMAL" : fr.model.Supplier.VATType; fr.model.Supplier.OurCustomerNumber = doc.buyer_references != null && doc.buyer_references.ContainsKey("customer_id") ? doc.buyer_references["customer_id"] : null; if(doc.payment_options != null) { // Loop payment options foreach(PaymentOption po in doc.payment_options) { // Get the name string name = po.name.ToUpper(); // Add information based on name if (name == "IBAN") { fr.model.Supplier.BIC = string.IsNullOrEmpty(po.bank_identifier_code) == false ? po.bank_identifier_code : fr.model.Supplier.BIC; fr.model.Supplier.IBAN = string.IsNullOrEmpty(po.account_reference) == false ? po.account_reference : fr.model.Supplier.IBAN; } else if (name == "BG") { fr.model.Supplier.BG = string.IsNullOrEmpty(po.account_reference) == false ? po.account_reference : fr.model.Supplier.BG; } else if (name == "PG") { fr.model.Supplier.PG = string.IsNullOrEmpty(po.account_reference) == false ? po.account_reference : fr.model.Supplier.PG; } else if (name == "BANK") { fr.model.Supplier.BankAccountNumber = string.IsNullOrEmpty(po.account_reference) == false ? po.account_reference.Replace(" ", "").Replace("-", "") : fr.model.Supplier.BankAccountNumber; fr.model.Supplier.Bank = string.IsNullOrEmpty(po.bank_name) == false ? po.bank_name : fr.model.Supplier.Bank; } } } // Add or update the supplier if (supplier_exists == true) { fr = await this.nox_client.Update<SupplierRoot>(fr.model, $"suppliers/{fr.model.Supplier.SupplierNumber}"); } else { fr = await this.nox_client.Add<SupplierRoot>(fr.model, "suppliers"); } // Log errors if (string.IsNullOrEmpty(fr.error) == false) { this.logger.LogError(fr.error); } // Return the post return fr.model; } // End of the UpsertSupplier method
} // End of the AddArticle method /// <summary> /// Add or update a customer /// </summary> public async Task<CustomerRoot> UpsertCustomer(string dox_email, AnnytabDoxTrade doc) { // Create variables FortnoxResponse<CustomerRoot> fr = new FortnoxResponse<CustomerRoot>(); bool customer_exists = false; string customer_email = doc.buyer_information != null && string.IsNullOrEmpty(doc.buyer_information.email) == false ? doc.buyer_information.email : dox_email; // Find customers on email FortnoxResponse<CustomersRoot> fr_page = await this.nox_client.Get<CustomersRoot>($"customers?email={customer_email}"); // Log errors if (string.IsNullOrEmpty(fr_page.error) == false) { this.logger.LogError(fr_page.error); } // Make sure that at least one customer was found if (fr_page.model != null && fr_page.model.Customers != null && fr_page.model.Customers.Count > 0) { // Get a customer fr = await this.nox_client.Get<CustomerRoot>($"customers/{fr_page.model.Customers[0].CustomerNumber}"); // Log errors if (string.IsNullOrEmpty(fr.error) == false) { this.logger.LogError(fr.error); } } // Check if the customer exists if (fr.model != null) { customer_exists = true; } else { fr.model = new CustomerRoot { Customer = new Customer() }; } // Update the customer: ONLY SET VAT TYPE, ACCOUNT IS SET IN ARTICLE fr.model.Customer.Email = customer_email; if (doc.seller_information != null) { fr.model.Customer.OurReference = string.IsNullOrEmpty(fr.model.Customer.OurReference) == true ? doc.seller_information.contact_name : fr.model.Customer.OurReference; } if(doc.buyer_information != null) { fr.model.Customer.Name = string.IsNullOrEmpty(doc.buyer_information.person_name) == false ? doc.buyer_information.person_name : fr.model.Customer.Name; fr.model.Customer.OrganisationNumber = string.IsNullOrEmpty(doc.buyer_information.person_id) == false ? doc.buyer_information.person_id : fr.model.Customer.OrganisationNumber; fr.model.Customer.VATNumber = string.IsNullOrEmpty(doc.buyer_information.vat_number) == false ? doc.buyer_information.vat_number : fr.model.Customer.VATNumber; fr.model.Customer.YourReference = string.IsNullOrEmpty(doc.buyer_information.contact_name) == false ? doc.buyer_information.contact_name : fr.model.Customer.YourReference; fr.model.Customer.Phone1 = string.IsNullOrEmpty(doc.buyer_information.phone_number) == false ? doc.buyer_information.phone_number : fr.model.Customer.Phone1; fr.model.Customer.Address1 = string.IsNullOrEmpty(doc.buyer_information.address_line_1) == false ? doc.buyer_information.address_line_1 : fr.model.Customer.Address1; fr.model.Customer.Address2 = string.IsNullOrEmpty(doc.buyer_information.address_line_2) == false ? doc.buyer_information.address_line_2 : fr.model.Customer.Address2; fr.model.Customer.ZipCode = string.IsNullOrEmpty(doc.buyer_information.postcode) == false ? doc.buyer_information.postcode : fr.model.Customer.ZipCode; fr.model.Customer.City = string.IsNullOrEmpty(doc.buyer_information.city_name) == false ? doc.buyer_information.city_name : fr.model.Customer.City; fr.model.Customer.CountryCode = string.IsNullOrEmpty(doc.buyer_information.country_code) == false ? doc.buyer_information.country_code : fr.model.Customer.CountryCode; fr.model.Customer.EmailOffer = string.IsNullOrEmpty(fr.model.Customer.EmailOffer) == true ? customer_email : fr.model.Customer.EmailOffer; fr.model.Customer.EmailOrder = string.IsNullOrEmpty(fr.model.Customer.EmailOrder) == true ? customer_email : fr.model.Customer.EmailOrder; fr.model.Customer.EmailInvoice = string.IsNullOrEmpty(fr.model.Customer.EmailInvoice) == true ? customer_email : fr.model.Customer.EmailInvoice; } if(doc.delivery_information != null) { fr.model.Customer.DeliveryName = string.IsNullOrEmpty(doc.delivery_information.person_name) == false ? doc.delivery_information.person_name : fr.model.Customer.DeliveryName; fr.model.Customer.DeliveryPhone1 = string.IsNullOrEmpty(doc.delivery_information.phone_number) == false ? doc.delivery_information.phone_number : fr.model.Customer.DeliveryPhone1; fr.model.Customer.DeliveryAddress1 = string.IsNullOrEmpty(doc.delivery_information.address_line_1) == false ? doc.delivery_information.address_line_1 : fr.model.Customer.DeliveryAddress1; fr.model.Customer.DeliveryAddress2 = string.IsNullOrEmpty(doc.delivery_information.address_line_2) == false ? doc.delivery_information.address_line_2 : fr.model.Customer.DeliveryAddress2; fr.model.Customer.DeliveryCity = string.IsNullOrEmpty(doc.delivery_information.city_name) == false ? doc.delivery_information.city_name : fr.model.Customer.DeliveryCity; fr.model.Customer.DeliveryCountryCode = string.IsNullOrEmpty(doc.delivery_information.country_code) == false ? doc.delivery_information.country_code : fr.model.Customer.DeliveryCountryCode; fr.model.Customer.DeliveryZipCode = string.IsNullOrEmpty(doc.delivery_information.postcode) == false ? doc.delivery_information.postcode : fr.model.Customer.DeliveryZipCode; } fr.model.Customer.Currency = string.IsNullOrEmpty(doc.currency_code) == false ? doc.currency_code : fr.model.Customer.Currency; fr.model.Customer.TermsOfDelivery = string.IsNullOrEmpty(doc.terms_of_delivery) == false ? doc.terms_of_delivery : fr.model.Customer.TermsOfDelivery; fr.model.Customer.TermsOfPayment = string.IsNullOrEmpty(doc.terms_of_payment) == false ? doc.terms_of_payment : fr.model.Customer.TermsOfPayment; fr.model.Customer.VATType = CommonTools.GetCustomerVatType(fr.model.Customer, this.default_values); fr.model.Customer.WayOfDelivery = string.IsNullOrEmpty(doc.mode_of_delivery) == false ? doc.mode_of_delivery : fr.model.Customer.WayOfDelivery; fr.model.Customer.Type = string.IsNullOrEmpty(fr.model.Customer.Type) == true && string.IsNullOrEmpty(fr.model.Customer.VATNumber) == false ? "COMPANY" : fr.model.Customer.Type; fr.model.Customer.PriceList = string.IsNullOrEmpty(fr.model.Customer.PriceList) == true ? this.default_values.PriceList : fr.model.Customer.PriceList; // Add or update the customer if (customer_exists == true) { fr = await this.nox_client.Update<CustomerRoot>(fr.model, $"customers/{fr.model.Customer.CustomerNumber}"); } else { fr = await this.nox_client.Add<CustomerRoot>(fr.model, "customers"); } // Log errors if (string.IsNullOrEmpty(fr.error) == false) { this.logger.LogError(fr.error); } // Return the post return fr.model; } // End of the UpsertCustomer method
} // End of the AddAccount method /// <summary> /// Add an article if it does not exist /// </summary> public async Task<ArticleRoot> AddArticle(ProductRow row) { // Create a reference to an article root FortnoxResponse<ArticleRoot> fr = new FortnoxResponse<ArticleRoot>(); // Make sure that the product code only consists of alphanumeric characters row.product_code = string.IsNullOrEmpty(row.product_code) == false ? CommonTools.ConvertToAlphanumeric(row.product_code) : null; // Find the article if (string.IsNullOrEmpty(row.gtin) == false) { // Try to get articles on EAN FortnoxResponse<ArticlesRoot> fr_page = await this.nox_client.Get<ArticlesRoot>($"articles?ean={row.gtin}"); // Log errors if (string.IsNullOrEmpty(fr_page.error) == false) { this.logger.LogError(fr_page.error); } // Make sure that at least one article was found if (fr_page.model != null && fr_page.model.Articles != null && fr_page.model.Articles.Count > 0) { // Get an article fr = await this.nox_client.Get<ArticleRoot>($"articles/{fr_page.model.Articles[0].ArticleNumber}"); // Log errors if (string.IsNullOrEmpty(fr.error) == false) { this.logger.LogError(fr.error); } } } if(fr.model == null && string.IsNullOrEmpty(row.manufacturer_code) == false) { // Try to get articles on manufacturer code FortnoxResponse<ArticlesRoot> fr_page = await this.nox_client.Get<ArticlesRoot>($"articles?manufacturerarticlenumber={row.manufacturer_code}"); // Log errors if (string.IsNullOrEmpty(fr_page.error) == false) { this.logger.LogError(fr_page.error); } // Make sure that at least one article was found if (fr_page.model != null && fr_page.model.Articles != null && fr_page.model.Articles.Count > 0) { // Get an article fr = await this.nox_client.Get<ArticleRoot>($"articles/{fr_page.model.Articles[0].ArticleNumber}"); // Log errors if (string.IsNullOrEmpty(fr.error) == false) { this.logger.LogError(fr.error); } } } if(fr.model == null && string.IsNullOrEmpty(row.product_code) == false) { // Get an article fr = await this.nox_client.Get<ArticleRoot>($"articles/{row.product_code}"); // Log errors if (string.IsNullOrEmpty(fr.error) == false) { this.logger.LogError(fr.error); } } // Add the article if it does not exist if (fr.model == null) { // Create a new article fr.model = new ArticleRoot { Article = new Article { ArticleNumber = string.IsNullOrEmpty(row.product_code) == false ? row.product_code : null, ConstructionAccount = this.default_values.SalesAccountSEREVERSEDVAT, Description = row.product_name, EAN = string.IsNullOrEmpty(row.gtin) == false ? row.gtin : null, EUAccount = this.default_values.SalesAccountEUREVERSEDVAT, EUVATAccount = this.default_values.SalesAccountEUVAT, ExportAccount = this.default_values.SalesAccountEXPORT, ManufacturerArticleNumber = string.IsNullOrEmpty(row.manufacturer_code) == false ? row.manufacturer_code : null, PurchaseAccount = this.default_values.PurchaseAccount, SalesAccount = CommonTools.GetArticleSalesAccount(row.vat_rate, this.default_values), Unit = string.IsNullOrEmpty(row.unit_code) == false ? row.unit_code : null } }; // Add an article fr = await this.nox_client.Add<ArticleRoot>(fr.model, "articles"); // Log errors if (string.IsNullOrEmpty(fr.error) == false) { this.logger.LogError(fr.error); } // Add a default price if (fr.model != null) { PriceRoot price = new PriceRoot { Price = new Price { ArticleNumber = fr.model.Article.ArticleNumber, PriceList = this.default_values.PriceList, FromQuantity = 0, Amount = row.unit_price } }; // Add a price FortnoxResponse<PriceRoot> fr_price = await this.nox_client.Add<PriceRoot>(price, "prices"); // Log errors if (string.IsNullOrEmpty(fr_price.error) == false) { this.logger.LogError(fr_price.error); } } } // Return the post return fr.model; } // End of the AddArticle method
} // End of the CreatePurchaseOrder method /// <summary> /// Create a invoice /// </summary> private async Task<AnnytabDoxTrade> CreateInvoice(CompanySettingsRoot company, InvoiceRoot root, CustomerRoot customer_root) { // Create a Annytab Dox Trade document AnnytabDoxTrade post = new AnnytabDoxTrade(); post.id = root.Invoice.DocumentNumber; post.document_type = root.Invoice.Credit == true ? "credit_invoice" : "invoice"; post.payment_reference = string.IsNullOrEmpty(root.Invoice.OCR) == false ? root.Invoice.OCR : root.Invoice.DocumentNumber; post.issue_date = root.Invoice.InvoiceDate; post.due_date = root.Invoice.DueDate; post.delivery_date = root.Invoice.DeliveryDate; post.seller_references = new Dictionary<string, string>(); post.seller_references.Add("quotation_id", root.Invoice.OfferReference); post.seller_references.Add("order_id", root.Invoice.OrderReference); post.seller_references.Add("invoice_id", root.Invoice.InvoiceReference); post.buyer_references = new Dictionary<string, string>(); post.buyer_references.Add("customer_id", root.Invoice.CustomerNumber); post.buyer_references.Add("order_id", root.Invoice.YourOrderNumber); post.terms_of_delivery = root.Invoice.TermsOfDelivery; post.terms_of_payment = root.Invoice.TermsOfPayment; post.mode_of_delivery = root.Invoice.WayOfDelivery; post.total_weight_kg = 0M; post.penalty_interest = this.default_values.PenaltyInterest; post.currency_code = root.Invoice.Currency; post.vat_country_code = company.CompanySettings.CountryCode; post.comment = root.Invoice.Remarks; post.seller_information = GetCompanyParty(company, root.Invoice.OurReference); post.buyer_information = new PartyInformation { person_id = root.Invoice.OrganisationNumber, person_name = root.Invoice.CustomerName, address_line_1 = root.Invoice.Address1, address_line_2 = root.Invoice.Address2, postcode = root.Invoice.ZipCode, city_name = root.Invoice.City, country_name = root.Invoice.Country, contact_name = root.Invoice.YourReference, phone_number = root.Invoice.Phone1, email = customer_root.Customer.Email }; post.delivery_information = new PartyInformation { person_name = root.Invoice.DeliveryName, address_line_1 = root.Invoice.DeliveryAddress1, address_line_2 = root.Invoice.DeliveryAddress2, postcode = root.Invoice.DeliveryZipCode, city_name = root.Invoice.DeliveryCity, country_name = root.Invoice.DeliveryCountry }; post.payment_options = GetPaymentOptions(company); post.product_rows = new List<ProductRow>(); foreach (InvoiceRow row in root.Invoice.InvoiceRows) { // Get the article FortnoxResponse<ArticleRoot> fr_article = await this.nox_client.Get<ArticleRoot>($"articles/{row.ArticleNumber}"); // Make sure that article root and article not is null if (fr_article.model == null || fr_article.model.Article == null) { fr_article.model = new ArticleRoot { Article = new Article() }; } // Add to the total weight post.total_weight_kg += fr_article.model.Article.Weight != null ? (fr_article.model.Article.Weight * row.DeliveredQuantity) / 1000M : 0; // Calculate the price decimal? price = root.Invoice.VATIncluded == true ? row.Price / ((100 + row.VAT) / 100) : row.Price; if (row.Discount > 0M && row.DiscountType == "AMOUNT") { if (root.Invoice.VATIncluded == true) { decimal? discount = row.Discount / ((100 + row.VAT) / 100); price = price - (discount / row.DeliveredQuantity); } else { price = price - (row.Discount / row.DeliveredQuantity); } } else if (row.Discount > 0M && row.DiscountType == "PERCENT") { price = price - (price * (row.Discount / 100)); } // Add a product row post.product_rows.Add(new ProductRow { product_code = fr_article.model.Article.ArticleNumber, manufacturer_code = fr_article.model.Article.ManufacturerArticleNumber, gtin = fr_article.model.Article.EAN, product_name = row.Description, vat_rate = row.VAT / 100, quantity = row.DeliveredQuantity, unit_code = row.Unit, unit_price = price, subrows = null }); } decimal? invoice_fee = AddInvoiceFee(root.Invoice.VATIncluded, root.Invoice.AdministrationFee, root.Invoice.AdministrationFeeVAT, post.product_rows, root.Invoice.Language); decimal? freight_fee = AddFreight(root.Invoice.VATIncluded, root.Invoice.Freight, root.Invoice.FreightVAT, post.product_rows, root.Invoice.Language); post.vat_specification = CommonTools.GetVatSpecification(post.product_rows); post.subtotal = root.Invoice.Net + invoice_fee + freight_fee; post.vat_total = root.Invoice.TotalVAT; post.rounding = root.Invoice.RoundOff; post.total = root.Invoice.Total; post.paid_amount = root.Invoice.TotalToPay - root.Invoice.Balance; post.balance_due = root.Invoice.Balance; // Return the post return post; } // End of the CreateInvoice method
public async Task TestAddPost() { // Create a post //SupplierInvoiceRoot post = new SupplierInvoiceRoot //{ // SupplierInvoice = new SupplierInvoice // { // SupplierNumber = "502", // InvoiceNumber = "D46", // InvoiceDate = "2019-08-06", // DueDate = "2019-08-21", // Currency = "SEK", // Total = 203M, // VAT = 40.625M, // RoundOffValue = -0.125M, // VATType = "NORMAL", // //SalesType = "STOCK", //Ett fel uppstod i lagermodulen (2003127) // SupplierInvoiceRows = new List<SupplierInvoiceRow> // { // new SupplierInvoiceRow // { // ArticleNumber = "GiB", // Account = "6210", // ItemDescription = "Gibibytes", // Quantity = 10, // Price = 8 // }, // new SupplierInvoiceRow // { // ArticleNumber = "File", // Account = "6210", // ItemDescription = "Filer", // Quantity = 33, // Price = 2.50M // } // } // } //}; SupplierInvoiceRoot post = new SupplierInvoiceRoot { SupplierInvoice = new SupplierInvoice { SupplierNumber = "502", InvoiceNumber = "D46", InvoiceDate = "2019-08-06", DueDate = "2019-08-21", Currency = "SEK", Total = -203M, VAT = -40.625M, RoundOffValue = 0.125M, VATType = "NORMAL", //SalesType = "STOCK", //Ett fel uppstod i lagermodulen (2003127) SupplierInvoiceRows = new List<SupplierInvoiceRow> { new SupplierInvoiceRow { ArticleNumber = "GiB", Account = "6210", ItemDescription = "Gibibytes", Quantity = -10, Price = 8 }, new SupplierInvoiceRow { ArticleNumber = "File", Account = "6210", ItemDescription = "Filer", Quantity = -33, Price = 2.50M } } } }; // Add the post FortnoxResponse<SupplierInvoiceRoot> fr = await config.fortnox_client.Add<SupplierInvoiceRoot>(post, "supplierinvoices"); // Log the error if (fr.model == null) { config.logger.LogError(fr.error); } // Test evaluation Assert.AreNotEqual(null, fr.model); } // End of the TestAddPost method