public void Create(DistributorCreate distributorCreate) { try { Distributor distributorToAdd = new Distributor { IsLocked = false, Counter = 0, //TankDistributors = new List<TankDistributor>() }; _context.Add(distributorToAdd); _context.SaveChanges(); //if (distributorCreate.TankIds == null) distributorCreate.TankIds = new List<int>(); foreach (int tankId in distributorCreate.TankIds) { TankDistributor td = _tankDistributorService.Create(tankId, distributorToAdd.DistributorId); distributorToAdd.TankDistributors.Add(td); _tankService.GetById(tankId).TankDistributors.Add(td); } _context.SaveChanges(); } catch (Exception ex) { throw new Exception(ex.ToString()); } }
public void Create(TransactionCreate transactionCreate) { try { if (transactionCreate.SumOfLoyaltyCardPoints > 0 && transactionCreate.Transaction.LoyaltyCardId != null && transactionCreate.Transaction.LoyaltyCardId != 0) { LoyaltyCard loyaltyCard = _loyaltyCardService.GetById((int)transactionCreate.Transaction.LoyaltyCardId); loyaltyCard.Points -= transactionCreate.SumOfLoyaltyCardPoints; _loyaltyCardService.Edit(loyaltyCard); } Invoice invoice = transactionCreate.Transaction.Invoice; if (invoice.NIP == 0 || invoice.InvoiceNumber == 0 || String.IsNullOrEmpty(invoice.RegistrationNumber) == true) { transactionCreate.Transaction.Invoice = null; } Transaction transaction = transactionCreate.Transaction; transaction.ApplicationUserId = _accountService.GetCurrentUserId(); transaction.ProductsLists = new List <ProductsList>(); transaction.Date = DateTime.Now; _context.Add(transaction); _context.SaveChanges(); foreach (var transactionProduct in transactionCreate.TransactionProduct) { if (transactionProduct.InTransaction) { var productList = _productsListService.CreateProductsLists(transaction.TransactionId, transactionProduct.ProductId, transactionProduct.Amount, transactionProduct.Price); transaction.ProductsLists.Add(productList); _productService.Edit(transactionProduct.ProductId, transactionProduct.Amount); if (transaction.LoyaltyCardId != null && transaction.LoyaltyCardId != 0) { Product product = _productService.GetById(transactionProduct.ProductId); LoyaltyCard loyaltyCard = _loyaltyCardService.GetById((int)transaction.LoyaltyCardId); var sumpoints = transactionProduct.Amount * product.LoyaltyPointsPrice; loyaltyCard.Points = (int)Math.Round((decimal)sumpoints); _loyaltyCardService.Edit(loyaltyCard); } } } foreach (var distributor in transactionCreate.DistributorInTransaction) { if (distributor.InTransaction) { Fueling fueling = new Fueling { TankId = distributor.TankId, DistributorId = distributor.DistributorId, Date = DateTime.Now, Amount = distributor.Counter, Price = distributor.Sum }; _fuelingService.Create(fueling); Tank tank = _tankService.GetById(distributor.TankId); tank.Stock = tank.Stock - distributor.Counter; _tankService.Edit(tank); Product product = _productService.GetById(tank.ProductId); var productList = _productsListService.CreateProductsLists(transaction.TransactionId, product.ProductId, distributor.Counter, distributor.PriceForLiter); transaction.ProductsLists.Add(productList); _productService.Edit(product.ProductId, distributor.Counter); if (transaction.LoyaltyCardId != null && transaction.LoyaltyCardId != 0) { LoyaltyCard loyaltyCard = _loyaltyCardService.GetById((int)transaction.LoyaltyCardId); var sumpoints = distributor.Counter * product.LoyaltyPointsPrice; loyaltyCard.Points = (int)Math.Round((decimal)sumpoints); _loyaltyCardService.Edit(loyaltyCard); } } } _context.SaveChanges(); } catch (Exception ex) { throw new Exception(ex.Message); } }