public async Task <HbResult <HbBrood> > Create(CreateBroodForm form) { var lot = await _dc.Lots .Include(u => u.Layings) .Include(u => u.Overheads) .Include(u => u.Purchases) .FirstOrDefaultAsync(u => u.Id == form.LotId && !u.IsDeleted); if (lot == null) { return(new HbResult <HbBrood>(ErrorCodes.LotNotFound)); } var existBroodsSum = await _dc.Broods.Where(u => u.LotId == form.LotId && !u.IsDeleted) .SumAsync(u => u.Count); var layingSum = lot.Layings.Where(u => !u.IsDeleted).Sum(u => u.Count); if (layingSum < existBroodsSum + form.Count) { return(new HbResult <HbBrood>(ErrorCodes.BroodAmountMoreThanLayingsSum)); } var broodPrice = lot.Overheads.Where(u => !u.IsDeleted).Sum(u => u.Amount) + lot.Purchases.Where(u => !u.IsDeleted).Sum(u => u.Amount); var brood = _dc.Broods.Add(new HbBroods { CreationDate = DateTimeOffset.UtcNow, BroodDate = form.BroodDate, Count = form.Count, DeadCount = form.DeadCount, EmptyCount = form.EmptyCount, LotId = form.LotId, DeadPercent = Math.Round(100m * form.DeadCount / layingSum, 2), EmptyPercent = Math.Round(100m * form.EmptyCount / layingSum, 2), Percent = Math.Round(100m * form.Count / layingSum, 2), PlacePrice = Math.Round(broodPrice / form.Count, 2) }); await _dc.SaveChangesAsync(); await _lotsUnit.RecalculateLot(form.LotId); return(new HbResult <HbBrood>(_mapper.Map <HbBrood>(brood.Entity))); }
public async Task <HbResult <HbLaying> > Create(CreateLayingForm form) { var lotExist = await _dc.Lots.Where(u => !u.IsDeleted && u.Id == form.LotId).AnyAsync(); if (!lotExist) { return(new HbResult <HbLaying>(ErrorCodes.LotNotFound)); } var incubatorExist = await _dc.Incubators.AnyAsync(u => !u.IsDeleted && u.Id == form.IncubatorId); if (!incubatorExist) { return(new HbResult <HbLaying>(ErrorCodes.IncubatorNotFound)); } var purchasesEggs = await _dc.Purchases.Where(u => u.LotId == form.LotId && !u.IsDeleted).SumAsync(u => u.Count); var existLayings = await _dc.Layings.Where(u => u.LotId == form.LotId && !u.IsDeleted).SumAsync(u => u.Count); if (purchasesEggs - existLayings < form.Count) { return(new HbResult <HbLaying>(ErrorCodes.LayingCountMoreThanPurchasesCount)); } var laying = _dc.Layings.Add(new HbLayings { Count = form.Count, LayingDate = form.LayingDate, LotId = form.LotId, IncubatorId = form.IncubatorId, CreationDate = DateTimeOffset.UtcNow }); await _dc.SaveChangesAsync(); await _lots.RecalculateLot(form.LotId); return(new HbResult <HbLaying>(_mapper.Map <HbLaying>(laying.Entity))); }
public async Task <HbResult <HbSale> > Create(CreateSaleForm form) { var lotExist = await _dc.Lots.Where(u => !u.IsDeleted && u.Id == form.LotId).AnyAsync(); if (!lotExist) { return(new HbResult <HbSale>(ErrorCodes.LotNotFound)); } if (form.Type != SalesTypes.Egg) { var soldChickens = await _dc.Sales.Where(u => u.LotId == form.LotId && u.Type != SalesTypes.Egg && !u.IsDeleted).SumAsync(u => u.Count); var broodCount = await _dc.Broods.Where(u => u.LotId == form.LotId && !u.IsDeleted).SumAsync(u => u.Count); if (form.Count > broodCount - soldChickens) { return(new HbResult <HbSale>(ErrorCodes.SalesCountMoreThanBroodCount)); } } var sale = _dc.Sales.Add(new HbSales { Amount = form.Amount, Buyer = form.Buyer, Comment = form.Comment, Count = form.Count, LotId = form.LotId, SaleDate = form.SaleDate, Type = form.Type }); await _dc.SaveChangesAsync(); await _lotsUnit.RecalculateLot(form.LotId); return(new HbResult <HbSale>(_mapper.Map <HbSale>(sale.Entity))); }
public async Task <HbResult <HbOverhead> > Create(CreateOverheadsForm form) { var lot = await _dc.Lots.FirstOrDefaultAsync(u => !u.IsDeleted && u.Id == form.LotId); if (lot == null) { return(new HbResult <HbOverhead>(ErrorCodes.LotNotFound)); } var res = _dc.Overheads.Add(new HbOverheads { Amount = form.Amount, Comment = form.Comment, LotId = form.LotId, OverheadDate = form.OverheadDate }); await _dc.SaveChangesAsync(); await _lotsUnit.RecalculateLot(form.LotId); return(new HbResult <HbOverhead>(_mapper.Map <HbOverhead>(res.Entity))); }
public async Task <HbResult <HbPurchase> > Create(CreatePurchaseForm form) { var lotExist = await _dc.Lots.AnyAsync(u => !u.IsDeleted && u.Id == form.LotId); if (!lotExist) { return(new HbResult <HbPurchase>(ErrorCodes.LotNotFound)); } var purchase = _dc.Purchases.Add(new HbPurchases { Address = form.Address, Amount = form.Amount, Count = form.Count, LotId = form.LotId, PurchaseDate = form.PurchaseDate }); await _dc.SaveChangesAsync(); await _lotsUnit.RecalculateLot(form.LotId); return(new HbResult <HbPurchase>(_mapper.Map <HbPurchase>(purchase.Entity))); }