Example #1
0
        public IHttpActionResult Put(OrdersModel model)
        {
            var order = manager.GetById(model.Id);

            var newOrder = new Orders()
            {
                AutoBill = order.AutoBill,
                City = order.City,
                Comment = order.Comment,
                CommunicationPartnerId = order.CommunicationPartnerId,
                CustomerId = order.CustomerId,
                Discount = order.Discount,
                Street = order.Street,
                Status = (int)OrderStatusTypes.Open,
                Zip = order.Zip,
                IsOffer = false,
                OrderNumber = numberProvider.GetNextOrderNumber(),
                Positions = new List<Positions>(),
                CreateDate = DateTime.Now,
                ChangeDate = DateTime.Now,
            };
            
            manager.AddEntity(newOrder);

            foreach(var position in order.Positions.Where(o => o.MaterialId.HasValue && !o.DeleteDate.HasValue).ToList())
            {
                var newPosition = new Positions()
                {
                    MaterialId = position.MaterialId.Value,
                    IsAlternative = position.IsAlternative,
                    IsMaterialPosition = position.IsMaterialPosition,
                    Amount = position.Amount,
                    Price = position.Price,
                    PaymentType = position.PaymentType,
                    Orders = newOrder
                };

                positionManager.AddEntity(newPosition);
                newOrder.Positions.Add(newPosition);
            }

            manager.SaveChanges();

            return Ok(new { id = newOrder.Id });
        }
Example #2
0
        public static void CalculateOrderPrices(Orders entity,
            ITaxesManager taxesManager,
            out double totalPriceWithoutDiscountWithoutTax,
            out double totalPriceWithoutTax,
            out double totalPrice,
            out double summaryPrice)
        {
            totalPriceWithoutDiscountWithoutTax = 0;
            totalPriceWithoutTax = 0;
            totalPrice = 0;

            var allPositions = entity.Positions.Where(o => !o.DeleteDate.HasValue).ToList();
            //Product prices
            foreach (var position in allPositions)
            {
                totalPriceWithoutDiscountWithoutTax += CalculatePositionPrice(position.Price, position.Amount, position.Payment);
            }

            var taxValue = CalculateTaxes(taxesManager);

            summaryPrice = CalculateTaxesAndDiscount(entity.Discount ?? 0, taxValue, entity.Customers.WithTaxes, null,
                ref totalPriceWithoutDiscountWithoutTax, ref totalPriceWithoutTax, ref totalPrice);
        }
Example #3
0
        public static double CalculateTotalPrice(Orders order,
            ITermPositionsManager termPositionsManager,
            IPositionsManager positionsManager,
            ITermCostsManager termCostsManager,
            ITaxesManager taxesManager,
            DateTime? fromDate,
            DateTime? toDate,
            ref double profit)
        {
            double result = 0;

            //TODO discuss with customer - take positions where proccessed amount not null (but take with 0)
            var termPositions = termPositionsManager.GetEntities(o => !o.DeleteDate.HasValue && o.Terms.OrderId == order.Id && o.ProccessedAmount.HasValue);

            if(fromDate.HasValue && toDate.HasValue)
            {
                termPositions = termPositions.Where(o => o.Terms.Date >= fromDate.Value && o.Terms.Date <= toDate.Value);
            }

            foreach (var termPosition in termPositions.ToList())
            {
                //positions
                if (termPosition.ProccessedAmount.Value > 0)
                {
                    var positionProfit = CalculatePositionPrice(termPosition.Positions.Price, termPosition.ProccessedAmount.Value,
                        termPosition.Positions.Payment);

                    result += positionProfit;

                    //todo calculate profit
                    profit += positionProfit;
                }

                //materials
                foreach (var material in termPosition.TermPositionMaterialRsps.Where(o => !o.DeleteDate.HasValue && o.Amount.HasValue))
                {
                    var amount = material.Amount.Value;
                    if (material.Materials.MaterialAmountTypes == MaterialAmountTypes.Meter)
                    {
                        if (material.Materials.Length != 0)
                        {
                            amount = amount / (double)material.Materials.Length.Value;
                        }
                        else
                        {
                            //todo
                        }
                    }

                    result += CalculatePositionPrice(material.Materials.Price, amount, PaymentTypes.Standard);

                    var materialProfit = material.Materials.Price * amount - material.Materials.BoughtPrice * amount;
                    profit += materialProfit;
                }
            }

            //material positions without terms
            var materialPositionsWithoutTerms = positionsManager.GetEntities(o => o.OrderId == order.Id && !o.DeleteDate.HasValue &&
                !o.TermId.HasValue && o.MaterialId.HasValue && o.IsMaterialPosition);

            if (fromDate.HasValue && toDate.HasValue)
            {
                materialPositionsWithoutTerms = materialPositionsWithoutTerms.Where(o => o.ChangeDate >= fromDate.Value && o.ChangeDate <= toDate.Value);
            }

            foreach (var position in materialPositionsWithoutTerms.ToList())
            {
                var price = CalculatePositionPrice(position.Price, position.Amount, position.Payment);

                result += price;

                profit += price - position.Materials.BoughtPrice * position.Amount;
            }

            //extra costs
            var termCosts = termCostsManager.GetEntities(o => !o.DeleteDate.HasValue && o.Terms.OrderId == order.Id);

            if (fromDate.HasValue && toDate.HasValue)
            {
                termCosts = termCosts.Where(o => o.Terms.Date >= fromDate.Value && o.Terms.Date <= toDate.Value);
            }

            foreach (var termCost in termCosts.ToList())
            {
                var price = CalculatePositionPrice(termCost.Price, 1, PaymentTypes.Standard);

                result += price;

                profit += price - termCost.Costs;
            }


            //TODO get taxes from invoices and calculate taxes only for open positions
            var taxes = CalculateTaxes(taxesManager);

            var taxValue = (result / (double)100) * taxes;
            if (order.Customers.WithTaxes)
            {
                //with taxes
                result += taxValue;
            }

            return result;
        }
Example #4
0
        private string ReplaceFooterTexts(string xmlMainXMLDoc, Orders order, Invoices invoice)
        {
            var xmlDoc = XDocument.Parse(xmlMainXMLDoc);
            var temp = xmlDoc.Descendants().LastOrDefault(o => o.Value.Contains("#PlanedPayDate"));
            var parentElement = GetParentElementByName(temp, "<w:tr ");

            var payDate = invoice.CreateDate.AddDays(invoice.PayInDays);

            //pay due information
            if (parentElement != null)
            {
                if (!String.IsNullOrEmpty(order.Customers.Iban) && !String.IsNullOrEmpty(order.Customers.Bic))
                {
                    temp = xmlDoc.Descendants().LastOrDefault(o => o.Value.Contains("#PayCashInterval"));
                    parentElement = GetParentElementByName(temp, "<w:tr ");
                    parentElement.Remove();
                    xmlMainXMLDoc = xmlDoc.Root.ToString();

                    xmlMainXMLDoc = xmlMainXMLDoc.Replace("#PlanedPayDate",
                        invoice.IsSellInvoice ? String.Empty : String.Format("am {0}", payDate.ToShortDateString()));
                }
                else
                {
                    parentElement.Remove();
                    xmlMainXMLDoc = xmlDoc.Root.ToString();

                    if (invoice.IsSellInvoice)
                    {
                        temp = xmlDoc.Descendants().LastOrDefault(o => o.Value.Contains("#PayCashInterval"));
                        parentElement = GetParentElementByName(temp, "<w:tr ");
                        parentElement.Remove();
                        xmlMainXMLDoc = xmlDoc.Root.ToString();
                    }
                    else
                    {
                        xmlMainXMLDoc = xmlMainXMLDoc.Replace("#PayCashInterval",
                            invoice.PayInDays == 0 ? "einem Tag" : String.Format("{0} Tage", invoice.PayInDays));
                    }
                }
            }


            //for sell Invoices
            xmlDoc = XDocument.Parse(xmlMainXMLDoc);
            temp = xmlDoc.Descendants().LastOrDefault(o => o.Value.Contains("#PayParts"));
            parentElement = GetParentElementByName(temp, "<w:tr ");

            if (parentElement != null)
            {
                if (invoice.IsSellInvoice)
                {
                    xmlMainXMLDoc = xmlMainXMLDoc.Replace("#PayParts",
                        String.Format("{0}% der Gesamtsumme bis {1} rein netto. {2}{3}% der Gesamtsumme nach Lieferung rein netto.",
                            invoice.PayPartOne.HasValue ? invoice.PayPartOne.Value : 75,
                            payDate.ToShortDateString(),
                            invoice.PayPartTwo.HasValue && invoice.PayPartTree.HasValue ?
                                String.Format("{0}% der Gesamtsumme bis {1} rein netto. ", invoice.PayPartTwo.Value,
                                payDate.AddDays(invoice.PayInDays).ToShortDateString()) : String.Empty,
                            invoice.PayPartTwo.HasValue && invoice.PayPartTree.HasValue ? invoice.PayPartTree.Value :
                                invoice.PayPartTwo.HasValue ? invoice.PayPartTwo.Value : 25
                        ));
                }
                else
                {
                    parentElement.Remove();
                    xmlMainXMLDoc = xmlDoc.Root.ToString();
                }
            }


            //Invoice without taxe
            xmlDoc = XDocument.Parse(xmlMainXMLDoc);
            temp = xmlDoc.Descendants().LastOrDefault(o => o.Value.Contains("#InvoiceWithoutTaxes"));
            parentElement = GetParentElementByName(temp, "<w:tr ");

            if (parentElement != null)
            {
                if (!invoice.WithTaxes)
                {
                    xmlMainXMLDoc = xmlMainXMLDoc.Replace("#InvoiceWithoutTaxes", String.Empty);
                }
                else
                {
                    parentElement.Remove();
                    xmlMainXMLDoc = xmlDoc.Root.ToString();
                }
            }

            return xmlMainXMLDoc;
        }
Example #5
0
        private string ReplaceUstId(string xmlMainXMLDoc, Orders order)
        {
            var xmlDoc = XDocument.Parse(xmlMainXMLDoc);
            var temp = xmlDoc.Descendants().LastOrDefault(o => o.Value.Contains("#CustomerUstId"));
            var parentElement = GetParentElementByName(temp, "<w:tr ");

            if (parentElement != null)
            {
                if (!String.IsNullOrEmpty(order.Customers.UstId))
                {
                    xmlMainXMLDoc = ReplaceFieldValue(xmlMainXMLDoc, "#CustomerUstId", order.Customers.UstId);
                }
                else
                {
                    parentElement.Remove();
                    xmlMainXMLDoc = xmlDoc.Root.ToString();
                }
            }
            return xmlMainXMLDoc;
        }
Example #6
0
        private string ReplaceRentAdditionalCostPositions(Orders order, string xmlMainXMLDoc)
        {
            if (order.Positions != null && order.Positions.Count != 0)
            {
                var positions = order.Positions.Where(o => !o.DeleteDate.HasValue && o.MaterialId.HasValue).ToList();
                var xmlDoc = XDocument.Parse(xmlMainXMLDoc);
                var temp = xmlDoc.Descendants().LastOrDefault(o => o.Value.Contains("#AdditionalCostDescription"));
                var parentTableElement = GetParentElementByName(temp, "<w:tr ");
                var prevElement = parentTableElement;

                var hasPauschalPosition = order.Positions.Any(o => !o.DeleteDate.HasValue && o.PaymentType == (int)PaymentTypes.Total);

                if (parentTableElement != null)
                {
                    foreach (var position in positions)
                    {
                        var price = Math.Round(position.Price * position.Amount, 2);

                        if (hasPauschalPosition)
                        {
                            price = 0;
                        }

                        var textElem = XElement.Parse(
                            ReplaceFieldValue(parentTableElement.ToString(), "#AdditionalCostDescription",
                                position.Materials.Name).
                            Replace("#AdditionalCostType",
                                String.Format("{0} x {1}", position.Amount, "Produkt")).
                            Replace("#AdditionalCostPrice", price.ToString("N2")));

                        prevElement.AddAfterSelf(textElem);
                        prevElement = textElem;
                    }

                    parentTableElement.Remove();
                }

                xmlMainXMLDoc = xmlDoc.Root.ToString();
                xmlMainXMLDoc = xmlMainXMLDoc.Replace("#AdditionalCostDescription", String.Empty);
                xmlMainXMLDoc = xmlMainXMLDoc.Replace("#AdditionalCostPrice", String.Empty);
            }
            else
            {
                xmlMainXMLDoc = xmlMainXMLDoc.Replace("#AdditionalCostDescription", String.Empty);
                xmlMainXMLDoc = xmlMainXMLDoc.Replace("#AdditionalCostPrice", String.Empty);
            }

            return xmlMainXMLDoc;
        }
Example #7
0
        private string ReplaceTotalPrice(Orders order, string xmlMainXMLDoc, ITaxesManager taxesManager)
        {
            if (order.Positions != null && order.Positions.Count != 0)
            {
                double totalPriceWithoutDiscountWithoutTax = 0;
                double totalPriceWithoutTax = 0;
                double totalPrice = 0;
                double summaryPrice = 0;

                CalculationHelper.CalculateOrderPrices(order, taxesManager, out totalPriceWithoutDiscountWithoutTax, out totalPriceWithoutTax,
                out totalPrice, out summaryPrice);

                xmlMainXMLDoc = xmlMainXMLDoc.Replace("#TotalPrice", summaryPrice.ToString("N2"));
            }
            else
            {
                xmlMainXMLDoc = xmlMainXMLDoc.Replace("#TotalPrice", String.Empty);
            }

            return xmlMainXMLDoc;
        }
Example #8
0
        private string ReplaceRentPositions(Orders order, string xmlMainXMLDoc, ITaxesManager taxesManager)
        {
            var positions = order.Positions != null ? order.Positions.Where(o => !o.DeleteDate.HasValue && o.ProductId.HasValue).ToList() :
                new List<Positions>();

            if (positions.Count != 0)
            {
                var minDate = DateTime.Now;
                var maxDate = DateTime.Now;
                var totalSellPrice = positions.Sum(o => o.Products.Price);

                if (positions.Any(o => o.Payment == PaymentTypes.Total))
                {
                    xmlMainXMLDoc = xmlMainXMLDoc.Replace("#RentPositionDescription", "Produkt gemäß Position 1");
                    xmlMainXMLDoc = xmlMainXMLDoc.Replace("#PaymentType", "Pauschal");

                    var totalPriceWithoutTax = positions.Sum(o => o.Price);
                    xmlMainXMLDoc = xmlMainXMLDoc.Replace("#RentPrice", totalPriceWithoutTax.ToString("N2"));

                    if (order.Customers.WithTaxes)
                    {
                        var taxes = CalculationHelper.CalculateTaxes(taxesManager);

                        var taxValue = (totalPriceWithoutTax / (double)100) * taxes;
                        //with taxes
                        var totalPrice = totalPriceWithoutTax + taxValue;

                        xmlMainXMLDoc = xmlMainXMLDoc.Replace("#BruttoPauschalPreis", totalPrice.ToString("N2"));
                    }
                    else
                    {
                        xmlMainXMLDoc = xmlMainXMLDoc.Replace("#BruttoPauschalPreis", totalPriceWithoutTax.ToString("N2"));
                    }
                }
                else
                {
                    xmlMainXMLDoc = ReplaceShortPositionDescription(positions, xmlMainXMLDoc);
                    xmlMainXMLDoc = xmlMainXMLDoc.Replace("#BruttoPauschalPreis", String.Empty);
                }

                xmlMainXMLDoc = xmlMainXMLDoc.Replace("#FromDate", minDate.ToShortDateString());
                xmlMainXMLDoc = xmlMainXMLDoc.Replace("#ToDate", maxDate.ToShortDateString());
                xmlMainXMLDoc = xmlMainXMLDoc.Replace("#LastPaymentDate", maxDate.AddDays(10).ToShortDateString());
                xmlMainXMLDoc = xmlMainXMLDoc.Replace("#TotalSellPrice", totalSellPrice.ToString("N2"));

                //todo xmlMainXMLDoc = ReplacePositionWithDescription(positions, xmlMainXMLDoc);
                xmlMainXMLDoc = xmlMainXMLDoc.Replace("#ProductDescription", String.Empty);
            }
            else
            {
                xmlMainXMLDoc = xmlMainXMLDoc.Replace("#ProductDescription", String.Empty);
                xmlMainXMLDoc = xmlMainXMLDoc.Replace("#FromDate", String.Empty);
                xmlMainXMLDoc = xmlMainXMLDoc.Replace("#ToDate", String.Empty);
                xmlMainXMLDoc = xmlMainXMLDoc.Replace("#LastPaymentDate", String.Empty);
                xmlMainXMLDoc = xmlMainXMLDoc.Replace("#BruttoPauschalPreis", String.Empty);
                xmlMainXMLDoc = xmlMainXMLDoc.Replace("#TotalSellPrice", String.Empty);
                xmlMainXMLDoc = xmlMainXMLDoc.Replace("#RentPositionDescription", String.Empty);
                xmlMainXMLDoc = xmlMainXMLDoc.Replace("#RentPrice", String.Empty);
            }

            return xmlMainXMLDoc;
        }
Example #9
0
        private string ReplaceBaseOfferFields(Orders order, string xmlMainXMLDoc)
        {
            xmlMainXMLDoc = ReplaceFieldValue(xmlMainXMLDoc, "#CustomerPhone", order.Customers.Phone);
            xmlMainXMLDoc = ReplaceFieldValue(xmlMainXMLDoc, "#CustomerFax", order.Customers.Fax);

            if (order.CommunicationPartners != null)
            {
                xmlMainXMLDoc = ReplaceFieldValue(xmlMainXMLDoc, "#CommunicationPartnerName",
                    String.Format("{0} {1}", order.CommunicationPartners.FirstName, order.CommunicationPartners.Name));
            }
            else
            {
                xmlMainXMLDoc = xmlMainXMLDoc.Replace("#CommunicationPartnerName", String.Empty);
            }

            var positions = order.Positions != null ? order.Positions.Where(o => !o.DeleteDate.HasValue && o.ProductId.HasValue).ToList() :
                new List<Positions>();

            if (positions.Count != 0)
            {
                var minDate = DateTime.Now;
                var maxDate = DateTime.Now;
                var duration = maxDate - minDate;
                xmlMainXMLDoc = xmlMainXMLDoc.Replace("#RentDuration", String.Format("{0} Tage", duration.Days));
            }
            else
            {
                xmlMainXMLDoc = xmlMainXMLDoc.Replace("#RentDuration", String.Empty);
            }

            return xmlMainXMLDoc;
        }
Example #10
0
        private string ReplaceBaseOrderFields(Orders order, string xmlMainXMLDoc)
        {
            xmlMainXMLDoc = ReplaceFieldValue(xmlMainXMLDoc, "#Street", order.Street);
            xmlMainXMLDoc = ReplaceFieldValue(xmlMainXMLDoc, "#ZIP", order.Zip.ToString());
            xmlMainXMLDoc = ReplaceFieldValue(xmlMainXMLDoc, "#City", order.City);

            return xmlMainXMLDoc;
        }
Example #11
0
 private string ReplaceCommonFields(Orders order, string xmlMainXMLDoc)
 {
     return ReplaceCommonFields(order.Customers, xmlMainXMLDoc);
 }
Example #12
0
        protected bool AddInvoicePositions(Orders order, Contracts.Entities.Invoices invoice)
        {
            var result = false;

            var allInvoicePositions = invoicePositionsManager.GetEntities(o => !o.DeleteDate.HasValue && 
                ((o.PositionId.HasValue && o.Positions.OrderId == order.Id) || (o.TermCostId.HasValue && o.TermCosts.Terms.OrderId == order.Id) ||
                 (o.TermPositionMaterialId.HasValue && o.TermPositionMaterialRsp.TermPositions.Terms.OrderId == order.Id))
                ).ToList();

            //TODO discuss with customer - take positions where proccessed amount not null (but take with 0)
            var termPositions = termPositionsManager.GetEntities(o => !o.DeleteDate.HasValue && o.Terms.OrderId == order.Id && o.ProccessedAmount.HasValue).ToList();

            foreach (var termPosition in termPositions)
            {
                var invoicePositions = allInvoicePositions.Where(o => o.PositionId.HasValue && o.PositionId.Value == termPosition.PositionId).ToList();

                double amount = 0;
                var oldAmount = invoicePositions.Sum(o => o.Amount);
                double usedAmount = termPositions.Where(o => o.PositionId == termPosition.PositionId).Sum(o => o.ProccessedAmount.Value);
                if (oldAmount == 0)
                {
                    amount = usedAmount;
                }
                else if (usedAmount > oldAmount)
                {
                    amount = usedAmount - oldAmount;
                }

                //positions
                if (amount > 0)
                {
                    var newPosition = new InvoicePositions()
                    {
                        PositionId = termPosition.PositionId,
                        Invoices = invoice,
                        Price = termPosition.Positions.Price,
                        Amount = amount,
                        CreateDate = DateTime.Now,
                        ChangeDate = DateTime.Now,
                        PaymentType = termPosition.Positions.PaymentType
                    };

                    invoice.InvoicePositions.Add(newPosition);

                    allInvoicePositions.Add(newPosition);

                    result = true;
                }

                //materials
                foreach (var material in termPosition.TermPositionMaterialRsps.Where(o => !o.DeleteDate.HasValue && o.Amount.HasValue))
                {
                    invoicePositions = allInvoicePositions.
                        Where(o => o.TermPositionMaterialId.HasValue && o.TermPositionMaterialId.Value == material.Id).ToList();


                    //old amount
                    oldAmount = invoicePositions.Sum(o => o.Amount);
                    //used amount
                    usedAmount = material.Amount.Value;

                    if (material.Materials.MaterialAmountTypes == MaterialAmountTypes.Meter)
                    {
                        if (material.Materials.Length != 0)
                        {
                            usedAmount = usedAmount / (double)material.Materials.Length.Value;
                        }
                        else
                        {
                            //todo
                        }
                    }


                    amount = 0;
                    if (oldAmount == 0)
                    {
                        amount = usedAmount;
                    }
                    else if (usedAmount > oldAmount)
                    {
                        amount = usedAmount - oldAmount;
                    }


                    if (amount > 0)
                    {
                        var newPosition = new InvoicePositions()
                        {
                            TermPositionMaterialId = material.Id,
                            Invoices = invoice,
                            Price = material.Materials.Price,
                            Amount = amount,
                            CreateDate = DateTime.Now,
                            ChangeDate = DateTime.Now,
                            PaymentType = (int)PaymentTypes.Standard
                        };

                        invoice.InvoicePositions.Add(newPosition);
                        result = true;
                    }
                }
            }

            //material positions without terms
            var materialPositionsWithoutTerms = positionsManager.GetEntities(o => o.OrderId == order.Id && !o.DeleteDate.HasValue &&
                !o.TermId.HasValue && o.MaterialId.HasValue && o.IsMaterialPosition).ToList();
            foreach (var position in materialPositionsWithoutTerms)
            {
                var invoicePositions = allInvoicePositions.
                        Where(o => o.PositionId.HasValue && o.PositionId.Value == position.Id).ToList();

                double amount = 0;
                //old amount
                var oldAmount = invoicePositions.Sum(o => o.Amount);
                double usedAmount = position.Amount;

                if (oldAmount == 0)
                {
                    amount = usedAmount;
                }
                else if (usedAmount > oldAmount)
                {
                    amount = usedAmount - oldAmount;
                }

                if (amount > 0)
                {
                    var newPosition = new InvoicePositions()
                    {
                        Positions = position,
                        Invoices = invoice,
                        Price = position.Materials.Price,
                        Amount = amount,
                        CreateDate = DateTime.Now,
                        ChangeDate = DateTime.Now,
                        PaymentType = (int)PaymentTypes.Standard
                    };

                    invoice.InvoicePositions.Add(newPosition);
                    result = true;
                }
            }

            //extra costs
            var termCosts = termCostsManager.GetEntities(o => !o.DeleteDate.HasValue && o.Terms.OrderId == order.Id).ToList();
            foreach (var termCost in termCosts)
            {
                var invoicePositions = allInvoicePositions.
                        Where(o => o.TermCostId.HasValue && o.TermCostId.Value == termCost.Id).ToList();

                double amount = 0;
                //old amount
                var oldAmount = invoicePositions.Sum(o => o.Amount);
                if (oldAmount == 0)
                {
                    amount = 1;
                }

                if (amount > 0)
                {
                    var newPosition = new InvoicePositions()
                    {
                        TermCosts = termCost,
                        Invoices = invoice,
                        Price = termCost.Price,
                        Amount = amount,
                        CreateDate = DateTime.Now,
                        ChangeDate = DateTime.Now,
                        PaymentType = (int)PaymentTypes.Standard
                    };

                    invoice.InvoicePositions.Add(newPosition);
                    result = true;
                }

            }

            return result;
        }