Exemplo n.º 1
0
        public bool UpdateDues(DealerProductDetailUpdateModel model)
        {
            using (var scope = new TransactionScope())
            {
                var         db   = Repository.Db as BusinessDbContext;
                AccountHead head = db.AccountHeads.First(x => x.ShopId == model.ShopId && x.Name == "Sale");
                model.Transaction.AccountHeadId       = head.Id;
                model.Transaction.AccountHeadName     = head.Name;
                model.Transaction.TransactionFlowType = TransactionFlowType.Income;
                model.Transaction.ParentId            = model.DealerId;
                model.Transaction.TransactionFor      = TransactionFor.Sale;
                model.Transaction.TransactionWith     = TransactionWith.Dealer;
                db.DealerProductTransactions.AddRange(model.DealerProductTransactions);
                db.Transactions.Add(model.Transaction);
                int changes = db.SaveChanges();

                foreach (var transaction in model.DealerProductTransactions)
                {
                    var dealerProduct = db.DealerProducts.First(x => x.Id == transaction.DealerProductId);
                    dealerProduct.Paid = db.DealerProductTransactions.Where(x => x.DealerProductId == dealerProduct.Id)
                                         .Sum(x => x.Amount);
                    dealerProduct.Due = dealerProduct.TotalPrice - dealerProduct.Paid;
                    db.SaveChanges();
                }

                DealerService dealerService = new DealerService(new BaseRepository <Dealer>(Repository.Db));
                bool          updatePoint   = dealerService.UpdateAmount(model.DealerId);

                scope.Complete();

                return(changes > 0);
            }
        }
        public IHttpActionResult UpdateDues(DealerProductDetailUpdateModel model)
        {
            DealerProductService service = new DealerProductService(new BaseRepository <DealerProduct>(BusinessDbContext.Create()));

            AddCommonValues(model, model.Transaction);

            foreach (var entity in model.DealerProductTransactions)
            {
                AddCommonValues(model, entity);
                entity.TransactionId = model.Transaction.Id;
            }

            bool updated = service.UpdateDues(model);

            return(Ok(updated));
        }