Beispiel #1
0
        public async Task TestUpdatePost()
        {
            // Create a post
            PriceRoot post = new PriceRoot
            {
                Price = new Price
                {
                    ArticleNumber = "1",
                    FromQuantity = 200M,
                    PriceList = "AD",
                    Amount = 30.33M
                }
            };

            // Update the post
            FortnoxResponse<PriceRoot> fr = await config.fortnox_client.Update<PriceRoot>(post, "prices/AD/1/200.00");

            // 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 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