Ejemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("CustomerId,CustomerName,NipString,ContactPerson,PhoneNumberString,Street,PostalCode,City")] Customers customers)
        {
            if (HttpContext.Session.GetString("EditCustomers") == "false")
            {
                return(RedirectToAction("Index", "Home"));
            }
            if (customers.CustomerName == null || customers.NipString == "" || customers.Street == null || customers.PostalCode == null || customers.City == null)
            {
                ViewBag.ErrorMessage = "Musisz uzupełnić wszystkie dane!";
                return(View(customers));
            }

            string[] postalCodeSplit = customers.PostalCode.Split("-");
            if (postalCodeSplit.Length != 2 || postalCodeSplit[0].Length != 2 || postalCodeSplit[1].Length != 3)
            {
                ViewBag.ErrorMessage = "Niepoprawny format kodu pocztowego!";
                return(View(customers));
            }
            if (customers.Nip < 1000000000)
            {
                ViewBag.ErrorMessage = "Niepoprawny numer NIP!";
                return(View(customers));
            }
            int customersWithSameNip = await _context.Customers.Where(c => c.Nip == customers.Nip).CountAsync();

            if (customersWithSameNip > 0)
            {
                ViewBag.ErrorMessage = "Istnieje już klient o takim numerze NIP!";
                return(View(customers));
            }
            if (customers.PhoneNumber < 100000000)
            {
                ViewBag.ErrorMessage = "Niepoprawny numer telefonu!";
                return(View(customers));
            }

            if (ModelState.IsValid)
            {
                _context.Add(customers);
                await _context.SaveChangesAsync();

                Addresses mainAddress = new Addresses();
                mainAddress.CustomerId    = customers.CustomerId;
                mainAddress.Street        = customers.Street;
                mainAddress.PostalCode    = customers.PostalCode;
                mainAddress.City          = customers.City;
                mainAddress.IsMainAddress = true;
                _context.Add(mainAddress);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(customers));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create([Bind("TaxRateId,Rate")] TaxRates taxRates)
        {
            if (HttpContext.Session.GetString("EditProducts") == "false")
            {
                return(RedirectToAction("Index", "Home"));
            }

            if (taxRates.Rate == null)
            {
                ViewBag.ErrorMessage = "Musisz podać stawkę!";
                return(View(taxRates));
            }

            int taxRatesWithSameRate = await _context.TaxRates.Where(t => t.Rate == taxRates.Rate).CountAsync();

            if (taxRatesWithSameRate > 0)
            {
                ViewBag.ErrorMessage = "Istnieje już taka stawka VAT!";
                return(View(taxRates));
            }

            if (ModelState.IsValid)
            {
                _context.Add(taxRates);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(taxRates));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Create([Bind("UserId,Username,Password,Firstname,Lastname,PermLevel")] Users users)
        {
            if (HttpContext.Session.GetString("EditUsers") == "false")
            {
                return(RedirectToAction("Index", "Home"));
            }

            if (users.Username == null || users.Firstname == null || users.Lastname == null || users.Password == null)
            {
                ViewBag.ErrorMessage  = "Musisz wypełnić wszystkie pola!";
                ViewData["PermLevel"] = new SelectList(_context.UserPermissions, "PermLevel", "PermName", users.PermLevel);
                return(View(users));
            }

            int usersWithSameUsername = await _context.Users.Where(u => u.Username == users.Username).CountAsync();

            if (usersWithSameUsername > 0)
            {
                ViewBag.ErrorMessage  = "Istnieje już użytkownik o takiej nazwie!";
                ViewData["PermLevel"] = new SelectList(_context.UserPermissions, "PermLevel", "PermName", users.PermLevel);
                return(View(users));
            }

            if (ModelState.IsValid)
            {
                users.Password = PasswordHelper.HashPassword(users.Password);
                _context.Add(users);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["PermLevel"] = new SelectList(_context.UserPermissions, "PermLevel", "PermName", users.PermLevel);
            return(View(users));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Create([Bind("PermLevel,PermName,EditUsers,EditProducts,EditCustomers,EditOrders,EditInvoices")] UserPermissions userPermissions)
        {
            if (HttpContext.Session.GetString("EditUsers") == "false")
            {
                return(RedirectToAction("Index", "Home"));
            }
            if (ModelState.IsValid)
            {
                _context.Add(userPermissions);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            var permissions = await _context.UserPermissions.ToListAsync();

            var lastLevel = permissions.Last().PermLevel;

            ViewBag.Permissions = permissions;
            ViewBag.PermLevel   = lastLevel + 1;

            return(View(userPermissions));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Create([Bind("ProductId,ProductName,BaseNetPrice,TaxRateId")] ProductDTO product)
        {
            if (HttpContext.Session.GetString("EditProducts") == "false")
            {
                return(RedirectToAction("Index", "Home"));
            }
            if (product.ProductName == null)
            {
                ViewBag.ErrorMessage  = "Nazwa produktu nie może być pusta!";
                ViewData["TaxRateId"] = new SelectList(_context.TaxRates.OrderBy(tr => tr.Rate), "TaxRateId", "Rate", product.TaxRateId);
                return(View(product));
            }

            int productsWithSameName = await _context.Products.Where(p => p.ProductName == product.ProductName).CountAsync();

            if (productsWithSameName > 0)
            {
                ViewBag.ErrorMessage  = "Istnieje już produkt o takiej nazwie!";
                ViewData["TaxRateId"] = new SelectList(_context.TaxRates.OrderBy(tr => tr.Rate), "TaxRateId", "Rate", product.TaxRateId);
                return(View(product));
            }

            if (ModelState.IsValid)
            {
                var productToInsert = new Products();
                productToInsert.ProductName  = product.ProductName;
                productToInsert.TaxRateId    = product.TaxRateId;
                productToInsert.BaseNetPrice = (int)(product.BaseNetPrice * 100);

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

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["TaxRateId"] = new SelectList(_context.TaxRates.OrderBy(tr => tr.Rate), "TaxRateId", "Rate", product.TaxRateId);
            return(View(product));
        }
Ejemplo n.º 6
0
        // GET: Orders/Create/5
        public async Task <IActionResult> Create(int?id, int?orderId, string?from)
        {
            if (HttpContext.Session.GetString("EditOrders") == "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());
            }
            ViewBag.From = from;

            Orders order;

            if (orderId == null)
            {
                order = new Orders();
                var lastOrderInDB = await _context.Orders
                                    .Where(o => o.OrderNumber.Contains("R"))
                                    .OrderByDescending(o => o.OrderId)
                                    .FirstOrDefaultAsync();

                string newOrderNumber;
                if (lastOrderInDB == null)
                {
                    newOrderNumber = "ZAM/R/0001";
                }
                else
                {
                    string[] lastOrderNumberSplit = lastOrderInDB.OrderNumber.Split('/');
                    int      numberInt            = int.Parse(lastOrderNumberSplit[2]);
                    string   numberString         = "" + (numberInt + 1);
                    while (numberString.Length < 4)
                    {
                        numberString = "0" + numberString;
                    }
                    newOrderNumber = "ZAM/R/" + numberString;
                }
                order.OrderNumber = newOrderNumber;
                order.OrderDate   = DateTime.Now;
                order.UserId      = int.Parse(HttpContext.Session.GetString("UserId"));
                order.CustomerId  = (int)id;
                order.AddressId   = customer.Addresses.First(a => a.IsMainAddress).AddressId;
                _context.Add(order);
                await _context.SaveChangesAsync();
            }
            else
            {
                order = await _context.Orders
                        .Include(o => o.OrderPositions)
                        .ThenInclude(op => op.Product)
                        .ThenInclude(p => p.TaxRate)
                        .FirstOrDefaultAsync(o => o.OrderId == orderId);
            }

            ViewBag.Customer      = customer;
            ViewData["AddressId"] = new SelectList(customer.Addresses, "AddressId", "FullAddress");

            return(View(order));
        }
Ejemplo n.º 7
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));
        }