예제 #1
0
        private void SaveInvoice()
        {
            Invoice.InvoiceId   = Invoices.Insert(Invoice);
            Invoice.TotalAmount = ProductsOnInvoice.Sum(x => x.Total);
            Invoice.User        = UserList.Single(x => x.UserId == Invoice.RefUserId);
            Invoice.Debitor     = SalesOrder.Debitor;

            SvenTechCollection <InvoicePosition> itemsToSave = new SvenTechCollection <InvoicePosition>();

            foreach (SalesOrderPosition item in ProductsOnInvoice)
            {
                itemsToSave.Add(new InvoicePosition(Invoice.InvoiceId, item.SalesOrderPositionId, (int)item.Quantity));
            }

            InvoicePositions.Insert(itemsToSave);

            InvoiceReportData invoiceReportData = new InvoiceReportData()
            {
                User       = Invoice.User,
                SalesOrder = SalesOrder,
                Invoice    = Invoice,
                MyCompany  = Globals.CoreData.MyCompany
            };

            SalesReportPDFCreator.CreateAndShowInvoiceReport(invoiceReportData, false);
        }
예제 #2
0
        public async Task <IActionResult> ChooseOrders(int?id, string?from, [Bind("Orders")] InvoiceDTO invoice)
        {
            if (HttpContext.Session.GetString("EditInvoices") == "false")
            {
                return(RedirectToAction("Index", "Home"));
            }
            if (id == null)
            {
                return(NotFound());
            }
            var customer = await _context.Customers.Include(c => c.Addresses).FirstOrDefaultAsync(c => c.CustomerId == id);

            if (customer == null)
            {
                return(NotFound());
            }
            string userIdString = HttpContext.Session.GetString("UserId");

            if (userIdString == null)
            {
                return(NotFound());
            }
            int userId = int.Parse(userIdString);
            var user   = await _context.Users.FindAsync(userId);

            if (user == null)
            {
                return(NotFound());
            }
            if (from != null)
            {
                ViewBag.From = from;
            }
            var ordersToInvoiceIds = new List <int>();

            invoice.Orders.ForEach(o => { if (o.Value)
                                          {
                                              ordersToInvoiceIds.Add(o.Key);
                                          }
                                   });
            if (ordersToInvoiceIds.Count == 0)
            {
                ViewBag.ErrorMessage = "Musisz wybrać przynajmniej jedno zamówienie, do którego chcesz wystawić fakturę!";
            }
            if (ViewBag.ErrorMessage == null)
            {
                var lastInvoiceInDB = await _context.Invoices
                                      .OrderByDescending(o => o.InvoiceId)
                                      .FirstOrDefaultAsync();

                string month = DateTime.Now.ToString("MM", DateTimeFormatInfo.InvariantInfo);
                string year  = DateTime.Now.ToString("yy", DateTimeFormatInfo.InvariantInfo);
                string newInvoiceNumber;
                if (lastInvoiceInDB == null)
                {
                    newInvoiceNumber = "F/001/" + month + "/" + year;
                }
                else
                {
                    string[] lastInvoiceNumberSplit = lastInvoiceInDB.InvoiceNumber.Split('/');
                    int      numberInt;
                    DateTime lastInvoiceDate = lastInvoiceInDB.InvoiceDate;
                    DateTime todayDate       = DateTime.Now;
                    if (lastInvoiceDate.Month == todayDate.Month && lastInvoiceDate.Year == todayDate.Year)
                    {
                        numberInt = int.Parse(lastInvoiceNumberSplit[1]);
                    }
                    else
                    {
                        numberInt = 1;
                    }
                    string numberString = "" + (numberInt + 1);
                    while (numberString.Length < 3)
                    {
                        numberString = "0" + numberString;
                    }
                    newInvoiceNumber = "F/" + numberString + "/" + month + "/" + year;
                }
                var newInvoice = new Invoices();
                newInvoice.InvoiceNumber = newInvoiceNumber;
                newInvoice.CustomerName  = customer.CustomerName;
                newInvoice.CustomerNip   = customer.Nip;
                var customerMainAddress = customer.Addresses.First(a => a.IsMainAddress);
                newInvoice.CustomerAddress    = customerMainAddress.Street;
                newInvoice.CustomerPostalCode = customerMainAddress.PostalCode;
                newInvoice.CustomerCity       = customerMainAddress.City;
                newInvoice.InvoiceDate        = DateTime.Now;
                newInvoice.DaysToPay          = invoice.DaysToPay;
                newInvoice.UserName           = user.Firstname + " " + user.Lastname;
                newInvoice.CustomerId         = customer.CustomerId;
                newInvoice.UserId             = userId;
                _context.Add(newInvoice);
                await _context.SaveChangesAsync();

                var ordersToInvoice = await _context.Orders
                                      .Where(o => ordersToInvoiceIds.Contains(o.OrderId))
                                      .Include(o => o.OrderPositions)
                                      .ThenInclude(op => op.Product)
                                      .ThenInclude(p => p.TaxRate)
                                      .ToListAsync();

                var newInvoicePositions = new List <InvoicePositions>();
                ordersToInvoice.ForEach(o =>
                {
                    foreach (OrderPositions op in o.OrderPositions)
                    {
                        var invoicePosition             = new InvoicePositions();
                        invoicePosition.ProductName     = op.Product.ProductName;
                        invoicePosition.ProductCount    = op.Count;
                        invoicePosition.ProductNetPrice = op.Product.BaseNetPrice;
                        invoicePosition.ProductTaxRate  = op.Product.TaxRate.Rate;
                        invoicePosition.Discount        = op.Discount;
                        invoicePosition.InvoiceId       = newInvoice.InvoiceId;
                        newInvoicePositions.Add(invoicePosition);
                    }
                });
                _context.AddRange(newInvoicePositions);
                await _context.SaveChangesAsync();

                ordersToInvoice.ForEach(o => o.InvoiceId = newInvoice.InvoiceId);
                _context.UpdateRange(ordersToInvoice);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            var ordersToChooseFrom = await _context.Orders
                                     .Where(o => !o.OrderNumber.Contains("R"))
                                     .Include(o => o.Address)
                                     .Include(o => o.User)
                                     .Include(o => o.OrderPositions)
                                     .ThenInclude(op => op.Product)
                                     .ThenInclude(p => p.TaxRate)
                                     .Where(o => o.CustomerId == id && o.InvoiceId == null && o.RealisationDate != null)
                                     .ToListAsync();

            ViewBag.Orders = ordersToChooseFrom;
            if (invoice.Orders.Count == 0)
            {
                ordersToChooseFrom.ForEach(o =>
                {
                    invoice.Orders.Add(new KeyValuePair <int, bool>(o.OrderId, false));
                });
            }
            invoice.CustomerName    = customer.CustomerName;
            invoice.CustomerNip     = customer.NipString;
            invoice.CustomerAddress = customer.Addresses.First(a => a.IsMainAddress).FullAddress;

            return(View("ChooseOrders", invoice));
        }
예제 #3
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);
        }