Пример #1
0
        public static void SaveInvoice(InvoiceModel invoiceModel)
        {
            if (UserHelper.UserID != null)
            {
                var storageDbEntities = new StorageDBEntities();

                Invoice invoice = new Invoice
                {
                    SupplierID  = invoiceModel.Supplier.ID,
                    RecipientID = invoiceModel.Recipient.ID,
                    Date        = invoiceModel.Date,
                    Type        = invoiceModel.Type,
                    Number      = invoiceModel.Number,
                    PriceType   = invoiceModel.PriceType,
                    UserID      = UserHelper.UserID.Value
                };

                foreach (ProductsInInvoiceModel productsInInvoiceModel in invoiceModel.Products.Where(p => p.ProductID > 0).ToList())
                {
                    ProductsInInvoice productsInInvoice = new ProductsInInvoice
                    {
                        Price     = productsInInvoiceModel.Price,
                        Quantity  = productsInInvoiceModel.Quantity,
                        ProductID = productsInInvoiceModel.ProductID
                    };

                    invoice.ProductsInInvoices.Add(productsInInvoice);
                }

                storageDbEntities.Invoices.AddObject(invoice);

                storageDbEntities.SaveChanges();
            }
        }
Пример #2
0
        public static List <ProductModel> GetProductList()
        {
            var storageDbEntities = new StorageDBEntities();

            List <ProductModel> list = (from p in storageDbEntities.Products
                                        where p.UserID == UserHelper.UserID
                                        select new ProductModel
            {
                ID = p.ID,
                Code = p.Code,
                Name = p.Name,
                WholesalePrice = p.WholesalePrice,
                ShallowWholesalePrice = p.ShallowWholesalePrice,
                RetailPrice = p.RetailPrice,
                Unit = p.Unit,
                Category = new CategoryModel
                {
                    ID = (int?)p.Category.ID,
                    Name = p.Category.Name
                }
            }).ToList();

            list = list.OrderBy(l => Convert.ToInt32(l.Code)).ToList();

            return(list);
        }
Пример #3
0
        public static bool ValidateUser(string userName, string password)
        {
            var storageDbEntities = new StorageDBEntities();

            User user = storageDbEntities.Users.Where(u => u.Username == userName && u.Password == password).FirstOrDefault();

            return(user != null);
        }
Пример #4
0
        public static string GetUserFullName(string userName)
        {
            var storageDbEntities = new StorageDBEntities();

            User user = storageDbEntities.Users.Where(u => u.Username == userName).FirstOrDefault();

            return(user != null?string.Format("{0} {1}", user.Name, user.Surname) : string.Empty);
        }
Пример #5
0
        public static int?GetUserID(string userName)
        {
            var storageDbEntities = new StorageDBEntities();

            User user = storageDbEntities.Users.Where(u => u.Username == userName).FirstOrDefault();

            return(user != null ? user.ID : (int?)null);
        }
Пример #6
0
        public static void DeleteClient(int id)
        {
            var storageDbEntities = new StorageDBEntities();

            Client client = storageDbEntities.Clients.Where(c => c.ID == id && c.UserID == UserHelper.UserID).FirstOrDefault();

            storageDbEntities.Clients.DeleteObject(client);

            storageDbEntities.SaveChanges();
        }
Пример #7
0
        public static void DeleteProduct(int id)
        {
            var storageDbEntities = new StorageDBEntities();

            Category category = storageDbEntities.Categories.Where(p => p.ID == id && p.UserID == UserHelper.UserID).FirstOrDefault();

            storageDbEntities.Categories.DeleteObject(category);

            storageDbEntities.SaveChanges();
        }
Пример #8
0
        public static int GetNextInvoiceNumber(string type)
        {
            var storageDbEntities = new StorageDBEntities();

            var currentNumber = (from inv in storageDbEntities.Invoices
                                 where inv.Type == type && inv.UserID == UserHelper.UserID
                                 select inv.Number).DefaultIfEmpty().Max();

            return(currentNumber + 1);
        }
Пример #9
0
        public static void UpdateProduct(int id, CategoryModel categoryModel)
        {
            var storageDbEntities = new StorageDBEntities();

            Category category = storageDbEntities.Categories.Where(c => c.ID == id && c.UserID == UserHelper.UserID).FirstOrDefault();

            if (category != null)
            {
                category.Name = categoryModel.Name;
            }

            storageDbEntities.SaveChanges();
        }
Пример #10
0
        public static bool IsActive(string userName)
        {
            var storageDbEntities = new StorageDBEntities();

            User user = storageDbEntities.Users.Where(u => u.Username == userName).FirstOrDefault();

            if (user != null)
            {
                return(user.ExpirationDate >= DateTime.Now);
            }

            return(false);
        }
Пример #11
0
        public static void UpdateUser(UserModel userModel)
        {
            var storageDbEntities = new StorageDBEntities();

            User user = storageDbEntities.Users.Where(u => u.ID == userModel.ID).FirstOrDefault();

            if (user != null)
            {
                user.Name    = userModel.Name;
                user.Surname = userModel.Surname;
            }

            storageDbEntities.SaveChanges();
        }
Пример #12
0
        public static List <CategoryModel> GetCategoryList()
        {
            var storageDbEntities = new StorageDBEntities();

            List <CategoryModel> list = (from c in storageDbEntities.Categories
                                         where c.UserID == UserHelper.UserID
                                         select new CategoryModel
            {
                ID = c.ID,
                Name = c.Name,
            }).ToList();

            return(list);
        }
Пример #13
0
        public static CategoryModel GetCategory(int id)
        {
            var storageDbEntities = new StorageDBEntities();

            CategoryModel category = (from c in storageDbEntities.Categories
                                      where c.ID == id && c.UserID == UserHelper.UserID
                                      select new CategoryModel
            {
                ID = c.ID,
                Name = c.Name,
            }).SingleOrDefault();

            return(category);
        }
Пример #14
0
        public static void UpdateClient(int id, ClientModel clientModel)
        {
            var storageDbEntities = new StorageDBEntities();

            Client client = storageDbEntities.Clients.Where(c => c.ID == id && c.UserID == UserHelper.UserID).FirstOrDefault();

            if (client != null)
            {
                client.Name      = clientModel.Name;
                client.Address   = clientModel.Address;
                client.Telephone = clientModel.Telephone;
            }

            storageDbEntities.SaveChanges();
        }
Пример #15
0
        public static UserModel GetUser(string userName)
        {
            var storageDbEntities = new StorageDBEntities();

            UserModel userModel = (from u in storageDbEntities.Users
                                   where u.Username == userName
                                   select new UserModel
            {
                ID = u.ID,
                Name = u.Name,
                Surname = u.Surname
            }).FirstOrDefault();

            return(userModel);
        }
Пример #16
0
        public static void UpdateInvoice(InvoiceModel invoiceModel)
        {
            var storageDbEntities = new StorageDBEntities();

            Invoice invoice = storageDbEntities.Invoices.Where(inv => inv.ID == invoiceModel.ID && inv.UserID == UserHelper.UserID).FirstOrDefault();

            if (invoice != null)
            {
                // updates invoice metadata
                invoice.SupplierID  = invoiceModel.Supplier.ID;
                invoice.RecipientID = invoiceModel.Recipient.ID;
                invoice.PriceType   = invoiceModel.PriceType;

                foreach (var productInInvoice in invoice.ProductsInInvoices.ToList())
                {
                    ProductsInInvoiceModel productsInInvoiceModel = invoiceModel.Products.Where(p => p.ProductID == productInInvoice.ProductID).FirstOrDefault();

                    // product was updated by user
                    if (productsInInvoiceModel != null)
                    {
                        productInInvoice.Price    = productsInInvoiceModel.Price;
                        productInInvoice.Quantity = productsInInvoiceModel.Quantity;

                        invoiceModel.Products.Remove(productsInInvoiceModel);
                    }
                    // product was deleted by user
                    else
                    {
                        storageDbEntities.ProductsInInvoices.DeleteObject(productInInvoice);
                    }
                }

                // products were created by user
                foreach (var productsInInvoiceModel in invoiceModel.Products.Where(p => p.ProductID > 0).ToList())
                {
                    ProductsInInvoice productsInInvoice = new ProductsInInvoice
                    {
                        Price     = productsInInvoiceModel.Price,
                        Quantity  = productsInInvoiceModel.Quantity,
                        ProductID = productsInInvoiceModel.ProductID
                    };

                    invoice.ProductsInInvoices.Add(productsInInvoice);
                }

                storageDbEntities.SaveChanges();
            }
        }
Пример #17
0
        public static List <InvoiceModel> GetInvoiceList(string type)
        {
            var storageDbEntities = new StorageDBEntities();

            List <InvoiceModel> list = (from inv in storageDbEntities.Invoices
                                        where inv.Type == type && inv.UserID == UserHelper.UserID
                                        select new InvoiceModel
            {
                ID = inv.ID,
                Number = inv.Number,
                Date = inv.Date
            }
                                        ).ToList();

            return(list);
        }
Пример #18
0
        public static List <ClientModel> GetClientList()
        {
            var storageDbEntities = new StorageDBEntities();

            List <ClientModel> list = (from c in storageDbEntities.Clients
                                       where c.UserID == UserHelper.UserID
                                       select new ClientModel
            {
                ID = c.ID,
                Name = c.Name,
                Address = c.Address,
                Telephone = c.Telephone
            }).ToList();

            return(list);
        }
Пример #19
0
        public static ClientModel GetClient(int id)
        {
            var storageDbEntities = new StorageDBEntities();

            ClientModel client = (from c in storageDbEntities.Clients
                                  where c.ID == id && c.UserID == UserHelper.UserID
                                  select new ClientModel
            {
                ID = c.ID,
                Name = c.Name,
                Address = c.Address,
                Telephone = c.Telephone
            }).SingleOrDefault();

            return(client);
        }
Пример #20
0
        public static void CreateProduct(CategoryModel categoryModel)
        {
            if (UserHelper.UserID != null)
            {
                var storageDbEntities = new StorageDBEntities();

                Category newCategory = new Category
                {
                    Name   = categoryModel.Name,
                    UserID = UserHelper.UserID.Value
                };

                storageDbEntities.Categories.AddObject(newCategory);

                storageDbEntities.SaveChanges();
            }
        }
Пример #21
0
        public static void DeleteInvoice(int id)
        {
            var storageDbEntities = new StorageDBEntities();

            Invoice invoice = storageDbEntities.Invoices.Where(inv => inv.ID == id && inv.UserID == UserHelper.UserID).FirstOrDefault();

            if (invoice != null)
            {
                foreach (ProductsInInvoice productsInInvoice in invoice.ProductsInInvoices.ToList())
                {
                    storageDbEntities.ProductsInInvoices.DeleteObject(productsInInvoice);
                    storageDbEntities.SaveChanges();
                }

                storageDbEntities.Invoices.DeleteObject(invoice);
                storageDbEntities.SaveChanges();
            }
        }
Пример #22
0
        public static void CreateClient(ClientModel clientModel)
        {
            if (UserHelper.UserID != null)
            {
                var storageDbEntities = new StorageDBEntities();

                Client newClient = new Client
                {
                    Name      = clientModel.Name,
                    Address   = clientModel.Address,
                    Telephone = clientModel.Telephone,
                    UserID    = UserHelper.UserID.Value
                };

                storageDbEntities.Clients.AddObject(newClient);

                storageDbEntities.SaveChanges();
            }
        }
Пример #23
0
        public static void UpdateProduct(int id, ProductModel productModel)
        {
            var storageDbEntities = new StorageDBEntities();

            Product product = storageDbEntities.Products.Where(p => p.ID == id && p.UserID == UserHelper.UserID).FirstOrDefault();

            if (product != null)
            {
                product.Code                  = productModel.Code;
                product.Name                  = productModel.Name;
                product.Unit                  = productModel.Unit;
                product.WholesalePrice        = productModel.WholesalePrice;
                product.ShallowWholesalePrice = productModel.ShallowWholesalePrice;
                product.RetailPrice           = productModel.RetailPrice;
                product.CategoryID            = productModel.Category.ID;
            }

            storageDbEntities.SaveChanges();
        }
Пример #24
0
        public static ProductModel GetProductByCode(string code)
        {
            var storageDbEntities = new StorageDBEntities();

            ProductModel product = (from p in storageDbEntities.Products
                                    where p.Code == code && p.UserID == UserHelper.UserID
                                    select new ProductModel
            {
                ID = p.ID,
                Code = p.Code,
                Name = p.Name,
                WholesalePrice = p.WholesalePrice,
                ShallowWholesalePrice = p.ShallowWholesalePrice,
                RetailPrice = p.RetailPrice,
                Unit = p.Unit
            }).SingleOrDefault();

            return(product);
        }
Пример #25
0
        public static List <ProductModel> GetProductListByFilter(int categoryID, string code, string name)
        {
            var storageDbEntities = new StorageDBEntities();

            IQueryable <Product> products = storageDbEntities.Products;

            if (categoryID != -1)
            {
                products = products.Where(p => p.CategoryID == categoryID);
            }

            if (!String.IsNullOrEmpty(code))
            {
                products = products.Where(p => p.Code == code);
            }

            if (!String.IsNullOrEmpty(name))
            {
                products = products.Where(p => p.Name.Contains(name));
            }

            List <ProductModel> list = products.Where(p => p.UserID == UserHelper.UserID).Select(p => new ProductModel
            {
                ID                    = p.ID,
                Code                  = p.Code,
                Name                  = p.Name,
                WholesalePrice        = p.WholesalePrice,
                ShallowWholesalePrice = p.ShallowWholesalePrice,
                RetailPrice           = p.RetailPrice,
                Unit                  = p.Unit,
                Category              = new CategoryModel
                {
                    ID   = (int?)p.Category.ID,
                    Name = p.Category.Name
                }
            }).ToList();

            list = list.OrderBy(l => Convert.ToInt32(l.Code)).ToList();

            return(list);
        }
Пример #26
0
        public static void CreateProduct(ProductModel productModel)
        {
            if (UserHelper.UserID != null)
            {
                var storageDbEntities = new StorageDBEntities();

                Product newProduct = new Product
                {
                    Code                  = productModel.Code,
                    Name                  = productModel.Name,
                    Unit                  = productModel.Unit,
                    WholesalePrice        = productModel.WholesalePrice,
                    ShallowWholesalePrice = productModel.ShallowWholesalePrice,
                    RetailPrice           = productModel.RetailPrice,
                    CategoryID            = productModel.Category.ID,
                    UserID                = UserHelper.UserID.Value
                };

                storageDbEntities.Products.AddObject(newProduct);

                storageDbEntities.SaveChanges();
            }
        }
Пример #27
0
        public static ProductModel GetProduct(int id)
        {
            var storageDbEntities = new StorageDBEntities();

            ProductModel product = (from p in storageDbEntities.Products
                                    where p.ID == id && p.UserID == UserHelper.UserID
                                    select new ProductModel
            {
                ID = p.ID,
                Code = p.Code,
                Name = p.Name,
                WholesalePrice = p.WholesalePrice,
                ShallowWholesalePrice = p.ShallowWholesalePrice,
                RetailPrice = p.RetailPrice,
                Unit = p.Unit,
                Category = new CategoryModel
                {
                    ID = (int?)p.Category.ID,
                    Name = p.Category.Name
                }
            }).SingleOrDefault();

            return(product);
        }
Пример #28
0
        public static bool IsProductCodeAvailable(string code)
        {
            var storageDbEntities = new StorageDBEntities();

            return(storageDbEntities.Products.Where(p => p.Code == code && p.UserID == UserHelper.UserID).FirstOrDefault() == null);
        }
Пример #29
0
        public static InvoiceModel GetInvoice(int id)
        {
            var storageDbEntities = new StorageDBEntities();

            Invoice invoice = storageDbEntities.Invoices.Where(inv => inv.ID == id && inv.UserID == UserHelper.UserID).FirstOrDefault();

            if (invoice != null)
            {
                InvoiceModel invoiceModel = new InvoiceModel
                {
                    Supplier = new ClientModel
                    {
                        ID        = invoice.Supplier.ID,
                        Name      = invoice.Supplier.Name,
                        Address   = invoice.Supplier.Address,
                        Telephone = invoice.Supplier.Telephone
                    },
                    Recipient = new ClientModel
                    {
                        ID        = invoice.Recipient.ID,
                        Name      = invoice.Recipient.Name,
                        Address   = invoice.Recipient.Address,
                        Telephone = invoice.Recipient.Telephone
                    },
                    Date      = invoice.Date,
                    Type      = invoice.Type,
                    Number    = invoice.Number,
                    PriceType = invoice.PriceType,
                    ID        = invoice.ID
                };

                foreach (ProductsInInvoice productsInInvoice in invoice.ProductsInInvoices.ToList())
                {
                    ProductsInInvoiceModel productsInInvoiceModel = new ProductsInInvoiceModel
                    {
                        ID       = productsInInvoice.ID,
                        Price    = productsInInvoice.Price,
                        Quantity = productsInInvoice.Quantity,
                        Total    = productsInInvoice.Price * Convert.ToDecimal(productsInInvoice.Quantity),
                        Product  = new ProductModel
                        {
                            ID   = productsInInvoice.Product.ID,
                            Name = productsInInvoice.Product.Name,
                            Code = productsInInvoice.Product.Code,
                            Unit = productsInInvoice.Product.Unit,
                        }
                    };

                    invoiceModel.Products.Add(productsInInvoiceModel);

                    invoiceModel.MasterTotal += productsInInvoiceModel.Total;
                }

                invoiceModel.Products = invoiceModel.Products.OrderBy(p => Convert.ToInt32(p.Product.Code)).ToList();

                for (int position = 0; position < invoiceModel.Products.Count; position++)
                {
                    invoiceModel.Products[position].Position = position + 1;
                }

                return(invoiceModel);
            }

            return(null);
        }