Example #1
0
        public int CloseCheck(CheckDto check)
        {
            using (var context = new CassaContext(conStrName))
            {
                using (var tran = new TransactionScope())
                {
                    //добавляем новый чек
                    var checkDao = new Check
                    {
                        CashboxId = check.CashboxId,
                        CheckId   = check.CheckId,
                        DateTM    = check.DateTM ?? DateTime.Now,
                        Summ      = check.Summ,
                    };
                    checkDao.Details = check.Details.Select(x => new CheckDetail
                    {
                        Check         = checkDao,
                        WareId        = x.WareId,
                        CheckDetailId = x.CheckDetailId,
                        Price         = x.Price,
                        Qty           = x.Qty
                    }).ToList();
                    context.Checks.Add(checkDao);
                    context.SaveChanges();
                    //проводим движение товара и обновляем остатки
                    foreach (var det in check.Details)
                    {
                        context.Moves.Add(new Move
                        {
                            Qty       = det.Qty,
                            WareId    = det.WareId,
                            Kind      = CassaConst.MoveKind.mkOun,
                            MoveDate  = DateTime.Now,
                            MoveDocId = check.CheckId,
                        });

                        var rem = context.WareRems.FirstOrDefault(x => x.WareId == det.WareId);
                        if (rem.Qty < det.Qty)
                        {
                            throw new ApplicationException(
                                      $"Не хватате товара {rem.Ware.WareName}: в наличие {rem.Qty} требуется {det.Qty}!");
                        }

                        rem.Qty -= det.Qty;
                    }
                    context.SaveChanges();
                    //throw new ApplicationException("test");
                    tran.Complete();
                    return(checkDao.CheckId);
                }
            }
        }
Example #2
0
 public void AddWare(WareDto ware)
 {
     using (var context = new CassaContext(conStrName))
     {
         context.Wares.Add(new Ware
         {
             Price    = ware.Price,
             WareName = ware.WareName,
         });
         context.SaveChanges();
     }
 }