/// <summary>
        /// Creates credit items and return them
        /// </summary>
        /// <param name="grossNetType"></param>
        /// <param name="taxType"></param>
        /// <param name="amount"></param>
        /// <param name="costAccount"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        public List <Credit> CreateCredits(GrossNetType grossNetType, TaxType taxType, decimal amount, CostAccount costAccount, string description = "")
        {
            if (costAccount == null)
            {
                throw new Exception("Bitte Kontenrahmen angeben.");
            }

            List <Credit> credits = new List <Credit>();
            Credit        credit  = new Credit(amount, costAccount.CostAccountId, 0);

            credit.CostAccount = costAccount;

            CalculateTax(grossNetType, taxType, amount, out decimal tax, out decimal amountWithoutTax);

            credit.CreditId = tempCreditDebitId;

            if (tax > 0)
            {
                Credit creditTax = new Credit(tax, taxType.RefCostAccount, 0);
                creditTax.CostAccount = CostAccounts.GetById(taxType.RefCostAccount);
                creditTax.RefCreditId = tempCreditDebitId;
                creditTax.CreditId    = ++tempCreditDebitId;
                creditTax.IsTax       = true;
                credits.Add(creditTax);
            }

            credit.Amount      = amountWithoutTax;
            credit.Description = description;
            credits.Add(credit);
            tempCreditDebitId++;

            return(credits);
        }
        public void CreateCreditDebit(GrossNetType grossNetType, BookingType bookingType, decimal amount, CostAccount costAccountCreditor, CostAccount costAccountDebitor, TaxType taxType, out List <Credit> credits, out List <Debit> debits)
        {
            credits = new List <Credit>();
            debits  = new List <Debit>();

            if (costAccountCreditor == null || costAccountDebitor == null)
            {
                return;
            }

            CalculateTax(grossNetType, taxType, amount, out decimal tax, out decimal amountWithoutTax);

            Debit  debit  = new Debit();
            Credit credit = new Credit();

            TaxType nonTax = TaxTypes.GetById(1);

            if (bookingType == BookingType.Invoice)
            {
                if (taxType.Description.IndexOf("Vorsteuer", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    credits.AddRange(CreateCredits(grossNetType, nonTax, tax + amountWithoutTax, costAccountCreditor));
                    debits.AddRange(CreateDebits(grossNetType, taxType, amount, costAccountDebitor));
                }
                else if (taxType.Description.IndexOf("Umsatzsteuer", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    credits.AddRange(CreateCredits(grossNetType, taxType, amount, costAccountCreditor));
                    debits.AddRange(CreateDebits(grossNetType, nonTax, tax + amountWithoutTax, costAccountDebitor));
                }
                else
                {
                    credits.AddRange(CreateCredits(grossNetType, nonTax, amount, costAccountCreditor));
                    debits.AddRange(CreateDebits(grossNetType, nonTax, amount, costAccountDebitor));
                }
            }
            else if (bookingType == BookingType.CreditAdvice)
            {
                // ToDo Check with Tobias !!!

                if (taxType.Description.IndexOf("Vorsteuer", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    credits.AddRange(CreateCredits(grossNetType, nonTax, amount * (-1), costAccountCreditor));
                    debits.AddRange(CreateDebits(grossNetType, taxType, amount, costAccountDebitor));
                }
                else if (taxType.Description.IndexOf("Umsatzsteuer", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    credits.AddRange(CreateCredits(grossNetType, taxType, amount, costAccountCreditor));
                    debits.AddRange(CreateDebits(grossNetType, nonTax, amount * (-1), costAccountDebitor));
                }
                else
                {
                    credits.AddRange(CreateCredits(grossNetType, nonTax, amount, costAccountCreditor));
                    debits.AddRange(CreateDebits(grossNetType, nonTax, amount * (-1), costAccountDebitor));
                }
            }
        }
 private void CalculateTax(GrossNetType grossNetType, TaxType taxType, decimal amount, out decimal tax, out decimal amountWithoutTax)
 {
     if (taxType != null && taxType.AmountOfTax > 0)
     {
         if (grossNetType == GrossNetType.Brutto)
         {
             tax = amount / (100 + taxType.AmountOfTax) * taxType.AmountOfTax;
             amountWithoutTax = amount - tax;
         }
         else
         {
             tax = amount / 100 * taxType.AmountOfTax;
             amountWithoutTax = amount;
         }
     }
     else
     {
         tax = 0;
         amountWithoutTax = amount;
     }
 }