Ejemplo n.º 1
0
        public async Task <IActionResult> DeleteConfirmed(int id, int UserId)
        {
            if (HttpContext.Session.GetString("EditUsers") == "false")
            {
                return(RedirectToAction("Index", "Home"));
            }

            var invoicesToUpdate = await _context.Invoices.Where(i => i.UserId == id).ToListAsync();

            invoicesToUpdate.ForEach(i => i.UserId = null);
            _context.UpdateRange(invoicesToUpdate);
            await _context.SaveChangesAsync();

            var ordersToUpdate = await _context.Orders.Where(o => o.UserId == id).ToListAsync();

            ordersToUpdate.ForEach(o => o.UserId = UserId);
            _context.UpdateRange(ordersToUpdate);
            await _context.SaveChangesAsync();

            var users = await _context.Users.FindAsync(id);

            _context.Users.Remove(users);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> DeleteConfirmed(int id)
        {
            if (HttpContext.Session.GetString("EditCustomers") == "false")
            {
                return(RedirectToAction("Index", "Home"));
            }
            var invoicesToUpdate = await _context.Invoices.Where(i => i.CustomerId == id).ToListAsync();

            invoicesToUpdate.ForEach(i => i.CustomerId = null);
            _context.UpdateRange(invoicesToUpdate);
            await _context.SaveChangesAsync();

            var ordersToDelete = await _context.Orders.Include(o => o.OrderPositions).Where(o => o.CustomerId == id).ToListAsync();

            var orderPositionsToDelete = new List <OrderPositions>();

            ordersToDelete.ForEach(o => orderPositionsToDelete.AddRange(o.OrderPositions));
            _context.OrderPositions.RemoveRange(orderPositionsToDelete);
            await _context.SaveChangesAsync();

            _context.Orders.RemoveRange(ordersToDelete);
            await _context.SaveChangesAsync();

            var customers = await _context.Customers.FindAsync(id);

            var addresses = await _context.Addresses.Where(a => a.CustomerId == customers.CustomerId).ToListAsync();

            _context.Addresses.RemoveRange(addresses);
            await _context.SaveChangesAsync();

            _context.Customers.Remove(customers);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Ejemplo n.º 3
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));
        }