Example #1
0
        public async Task <HbResult <HbBrood> > Update(UpdateBroodForm form)
        {
            var brood = await _dc.Broods.FirstOrDefaultAsync(u => !u.IsDeleted && u.Id == form.Id);

            if (brood == null)
            {
                return(new HbResult <HbBrood>(ErrorCodes.BroodNotFound));
            }

            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.Id != form.Id && !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);

            brood.BroodDate    = form.BroodDate;
            brood.Count        = form.Count;
            brood.DeadCount    = form.DeadCount;
            brood.EmptyCount   = form.EmptyCount;
            brood.LotId        = form.LotId;
            brood.DeadPercent  = Math.Round(100m * form.DeadCount / layingSum, 2);
            brood.EmptyPercent = Math.Round(100m * form.EmptyCount / layingSum, 2);
            brood.Percent      = Math.Round(100m * form.Count / layingSum, 2);
            brood.PlacePrice   = Math.Round(broodPrice / form.Count, 2);

            await _dc.SaveChangesAsync();

            await _lotsUnit.RecalculateLot(brood.LotId);

            return(new HbResult <HbBrood>(_mapper.Map <HbBrood>(brood)));
        }
Example #2
0
        public async Task <IActionResult> Edit(UpdateBroodForm form)
        {
            if (!ModelState.IsValid)
            {
                await InitLotsList(form);

                return(View(form));
            }

            var res = await _broods.Update(form);

            if (res.IsCorrect)
            {
                return(RedirectToAction(nameof(List)));
            }

            ViewData[ViewDataKeys.ErrorMessage] = res.ErrorMessage;
            await InitLotsList(form);

            return(View(form));
        }