Beispiel #1
0
        public async Task <IActionResult> Create(int?id, [Bind("OrderId,OrderNumber,OrderDate,RealisationDate,InvoiceId,UserId,CustomerId,AddressId")] Orders orders)
        {
            if (HttpContext.Session.GetString("EditOrders") == "false")
            {
                return(RedirectToAction("Index", "Home"));
            }
            var orderPositions = await _context.OrderPositions
                                 .Include(op => op.Product)
                                 .ThenInclude(p => p.TaxRate)
                                 .Where(op => op.OrderId == orders.OrderId)
                                 .ToListAsync();

            if (orderPositions.Count == 0)
            {
                ViewBag.ErrorMessage = "Nie możesz zapisać zamówienia bez pozycji!";
            }
            if (orderPositions.Count > 0)
            {
                Orders orderToAdd    = new Orders();
                var    lastOrderInDB = await _context.Orders
                                       .Where(o => !o.OrderNumber.Contains("R"))
                                       .OrderByDescending(o => o.OrderId)
                                       .FirstOrDefaultAsync();

                string month = DateTime.Now.ToString("MM", DateTimeFormatInfo.InvariantInfo);
                string year  = DateTime.Now.ToString("yy", DateTimeFormatInfo.InvariantInfo);
                string newOrderNumber;
                if (lastOrderInDB == null)
                {
                    newOrderNumber = "ZAM/0001/" + month + "/" + year;
                }
                else
                {
                    string[] lastOrderNumberSplit = lastOrderInDB.OrderNumber.Split('/');
                    int      numberInt;
                    DateTime lastOrderDate = lastOrderInDB.OrderDate;
                    DateTime todayDate     = DateTime.Now;
                    if (lastOrderDate.Month == todayDate.Month && lastOrderDate.Year == todayDate.Year)
                    {
                        numberInt = int.Parse(lastOrderNumberSplit[1]);
                    }
                    else
                    {
                        numberInt = 1;
                    }

                    string numberString = "" + (numberInt + 1);
                    while (numberString.Length < 4)
                    {
                        numberString = "0" + numberString;
                    }
                    newOrderNumber = "ZAM/" + numberString + "/" + month + "/" + year;
                }
                orderToAdd.OrderNumber = newOrderNumber;
                orderToAdd.OrderDate   = DateTime.Now;
                orderToAdd.UserId      = orders.UserId;
                orderToAdd.CustomerId  = orders.CustomerId;
                orderToAdd.AddressId   = orders.AddressId;

                _context.Add(orderToAdd);
                await _context.SaveChangesAsync();

                List <OrderPositions> newOrderPositions = new List <OrderPositions>();
                orderPositions.ForEach(op =>
                {
                    var newPosition       = new OrderPositions();
                    newPosition.OrderId   = orderToAdd.OrderId;
                    newPosition.ProductId = op.ProductId;
                    newPosition.Count     = op.Count;
                    newPosition.Discount  = op.Discount;
                    newOrderPositions.Add(newPosition);
                });
                _context.RemoveRange(orderPositions);
                await _context.SaveChangesAsync();

                _context.AddRange(newOrderPositions);
                await _context.SaveChangesAsync();

                _context.Remove(orders);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            Orders order;

            order = await _context.Orders
                    .Include(o => o.OrderPositions)
                    .ThenInclude(op => op.Product)
                    .ThenInclude(p => p.TaxRate)
                    .FirstOrDefaultAsync(o => o.OrderId == orders.OrderId);

            var customer = await _context.Customers.Include(c => c.Addresses).FirstOrDefaultAsync(c => c.CustomerId == order.CustomerId);

            if (customer == null)
            {
                return(NotFound());
            }

            ViewBag.Customer      = customer;
            ViewData["AddressId"] = new SelectList(customer.Addresses, "AddressId", "FullAddress");
            return(View(order));
        }
Beispiel #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));
        }