コード例 #1
0
        public void CreateDelivare(WaybillBM model)
        {
            using (var transaction = context.Database.BeginTransaction())
            {
                try
                {
                    Waybill element = new Waybill
                    {
                        Date            = model.Date,
                        StockId         = model.StockId,
                        TypeOfWaybillId = Convert.ToInt32(3 + ""),
                        Summa           = model.Summa,
                        ShopHallId      = model.ShopHallId
                    };
                    context.Waybills.Add(element);
                    context.SaveChanges();
                    CountOst(model);

                    // убираем дубли по компонентам

                    /* var groupProducts = model.ProductWaybills
                     *                           .GroupBy(rec => rec.ProductId)
                     *                           .Select(rec => new
                     *                           {
                     *                               ProductId = rec.Key,
                     *                               Count = rec.Sum(r => r.Count)
                     *                           });
                     * // добавляем компоненты
                     * foreach (var groupProduct in groupProducts)
                     * {
                     *   context.ProductWaybills.Add(new ProductWaybill
                     *   {
                     *       WaybillId = element.Id,
                     *       ProductId = groupProduct.ProductId,
                     *       Count = groupProduct.Count
                     *   });
                     *   context.SaveChanges();
                     * }*/
                    foreach (var ProductWaybill in model.ProductWaybills)
                    {
                        context.ProductWaybills.Add(new ProductWaybill
                        {
                            WaybillId = element.Id,
                            ProductId = ProductWaybill.ProductId,
                            Count     = ProductWaybill.Count
                        });
                        context.SaveChanges();
                    }
                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
コード例 #2
0
        public void CreateUser(User model)
        {
            User element = context.Users.FirstOrDefault(rec => rec.Login == model.Login);

            if (element != null)
            {
                throw new Exception("Уже есть сотрудник с таким login");
            }
            context.Users.Add(new User
            {
                Login    = model.Login,
                Password = model.Password,
                FIO      = model.FIO,
                Role     = model.Role
            });
            context.SaveChanges();
        }
コード例 #3
0
        public void CreateType(TypeBM model)
        {
            TypeOfWaybill element = context.TypeOfWaybills.FirstOrDefault(rec => rec.Type == model.Type);

            if (element != null)
            {
                throw new Exception("Уже есть type с таким name");
            }
            context.TypeOfWaybills.Add(new TypeOfWaybill
            {
                Type = model.Type
            });
            context.SaveChanges();
        }
コード例 #4
0
        public void CreateShopHall(ShopHallBM model)
        {
            ShopHall element = context.ShopHalls.FirstOrDefault(rec => rec.Name == model.Name);

            if (element != null)
            {
                throw new Exception("Уже есть shophall с таким name");
            }
            context.ShopHalls.Add(new ShopHall
            {
                Name = model.Name
            });
            context.SaveChanges();
        }
コード例 #5
0
        public void CreateStock(StockBM model)
        {
            Stock element = context.Stocks.FirstOrDefault(rec => rec.Name == model.Name);

            if (element != null)
            {
                throw new Exception("Уже есть stock с таким name");
            }
            context.Stocks.Add(new Stock
            {
                Name = model.Name
            });
            context.SaveChanges();
        }
コード例 #6
0
        public void CreateProdGroup(ProductGroupBM model)
        {
            ProdGroup element = context.ProdGroups.FirstOrDefault(rec => rec.Name == model.Name);

            if (element != null)
            {
                throw new Exception("Уже есть группа с таким name");
            }
            context.ProdGroups.Add(new ProdGroup
            {
                Name = model.Name,
                Norm = model.Norm
            });
            context.SaveChanges();
        }
コード例 #7
0
        public string GetDataFromStookKeeper(DateTime date)
        {
            DataContractJsonSerializer contractJS = new DataContractJsonSerializer(typeof(List <Waybill>));
            MemoryStream   msContract             = new MemoryStream();
            List <Waybill> resultContract         = context.Waybills.Where(c => c.Date < date & c.TypeOfWaybillId == 5).ToList();

            contractJS.WriteObject(msContract, resultContract);
            msContract.Position = 0;
            StreamReader srContract    = new StreamReader(msContract);
            string       contractsJSON = srContract.ReadToEnd();

            srContract.Close();
            msContract.Close();


            using (var transaction = context.Database.BeginTransaction())
            {
                try
                {
                    foreach (Waybill el in resultContract)
                    {
                        // удаяем записи по компонентам при удалении изделия
                        context.ProductWaybills.RemoveRange(
                            context.ProductWaybills.Where(rec => rec.WaybillId == el.Id));

                        context.Waybills.Remove(el);
                        context.SaveChanges();
                    }
                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw;
                }
            }



            return
                ("{\n" + "    \"Waybills\": " + contractsJSON + "}");
        }
コード例 #8
0
        public void CreateProduct(ProductBM model)
        {
            Product element = context.Products.FirstOrDefault(rec => rec.Name == model.Name || rec.Number == model.Number);

            if (element != null)
            {
                throw new Exception("Уже есть product с таким name or number");
            }
            context.Products.Add(new Product
            {
                Name        = model.Name,
                Price       = model.Price,
                ProdGroupId = model.ProdGroupId,
                Mark        = model.Mark,
                AdvInf      = model.AdvInf,
                Producer    = model.Producer,
                Provider    = model.Provider,
                Number      = model.Number
            });
            context.SaveChanges();
        }
コード例 #9
0
        public void CreateReval(WaybillBM model, double nPrice)
        {
            using (var transaction = context.Database.BeginTransaction())
            {
                try
                {
                    Waybill element = new Waybill
                    {
                        Date            = model.Date,
                        TypeOfWaybillId = Convert.ToInt32(5 + ""),

                        Summa = CalcSum(model, nPrice)
                    };
                    context.Waybills.Add(element);
                    context.SaveChanges();
                    // убираем дубли по компонентам

                    /* var groupProducts = model.ProductWaybills
                     *                           .GroupBy(rec => rec.ProductId)
                     *                           .Select(rec => new
                     *                           {
                     *                               ProductId = rec.Key,
                     *                               Count = rec.Sum(r => r.Count)
                     *                           });
                     * // добавляем компоненты
                     * foreach (var groupProduct in groupProducts)
                     * {
                     *   context.ProductWaybills.Add(new ProductWaybill
                     *   {
                     *       WaybillId = element.Id,
                     *       ProductId = groupProduct.ProductId,
                     *       Count = groupProduct.Count
                     *   });
                     *   context.SaveChanges();
                     * }*/
                    foreach (var ProductWaybill in model.ProductWaybills)
                    {
                        ProductWaybill productWaybill = new ProductWaybill
                        {
                            WaybillId = element.Id,
                            ProductId = ProductWaybill.ProductId,
                            Count     = ProductWaybill.Count
                        };
                        context.ProductWaybills.Add(productWaybill);
                        context.SaveChanges();
                    }

                    int id = model.ProductWaybills[0].ProductId;

                    /*Product productUpd = context.Products.FirstOrDefault(rec => rec.Id == id);
                     * if (productUpd == null)
                     * {
                     *  throw new Exception("Элемент не найден");
                     * }
                     * //   Product p = new Product { Price = nPrice };
                     *
                     * productUpd.Price = nPrice;
                     * productUpd.Producer = "fgfj";
                     * context.SaveChanges();*/


                    foreach (ProductWaybillBM pw in model.ProductWaybills)
                    {
                        Product productUpd = context.Products.FirstOrDefault(rec => rec.Id == pw.ProductId);
                        if (productUpd == null)
                        {
                            throw new Exception("Элемент не найден");
                        }
                        //   Product p = new Product { Price = nPrice };

                        productUpd.Price = nPrice;
                        context.SaveChanges();
                    }
                    // context.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }