public IActionResult CloseDefferedSale(int id, decimal cashSum, decimal cashlessSum, int moneyWorkerId, int userId)
        {
            Sale sale = _saleService.All().First(b => b.Id == id);
            User user = _userService.All().FirstOrDefault(x => x.Id == userId);

            if (cashSum > 0)
            {
                _infoMoneyService.Create(new InfoMoney()
                {
                    PaymentType        = PaymentType.Cash,
                    MoneyWorkerId      = user.ShopId,
                    MoneyOperationType = MoneyOperationType.Sale,
                    Sum  = cashSum,
                    Sale = sale
                });
            }

            if (cashlessSum > 0)
            {
                _infoMoneyService.Create(new InfoMoney()
                {
                    PaymentType        = PaymentType.Cashless,
                    MoneyWorkerId      = moneyWorkerId,
                    MoneyOperationType = MoneyOperationType.Sale,
                    Sum  = cashlessSum,
                    Sale = sale
                });
            }

            return(RedirectToAction("CheckPrint", new { saleId = id, operationSum = cashSum + cashlessSum }));
        }
        public IActionResult BookingClose(int id, decimal cashSum, decimal cashlessSum, int moneyWorkerId, bool cashless)
        {
            var  userName = HttpContext.User.Identity.Name;
            User user     = _userService.All().First(u => u.Login == userName);

            if (user == null)
            {
                throw new Exception("Пользователь не найден");
            }

            Booking booking = _bookingService.All().First(b => b.Id == id);

            booking.Debt -= (cashSum + cashlessSum);
            booking.Pay  += (cashSum + cashlessSum);

            if (booking.Debt <= 0)
            {
                booking.Status = BookingStatus.Close;
            }

            _bookingService.Update(booking);

            if (cashSum > 0)
            {
                _infoMoneyService.Create(new InfoMoney()
                {
                    PaymentType        = PaymentType.Cash,
                    MoneyWorkerId      = user.ShopId,
                    MoneyOperationType = MoneyOperationType.Booking,
                    Sum       = cashSum,
                    BookingId = booking.Id
                });
            }

            if (cashlessSum > 0)
            {
                _infoMoneyService.Create(new InfoMoney()
                {
                    PaymentType        = PaymentType.Cashless,
                    MoneyWorkerId      = moneyWorkerId,
                    MoneyOperationType = MoneyOperationType.Booking,
                    Sum       = cashlessSum,
                    BookingId = booking.Id
                });
            }

            if (booking.Status != BookingStatus.Close)
            {
                return(RedirectToAction("CheckPrint",
                                        new { bookingId = booking.Id, operationSum = cashSum + cashlessSum }));
            }

            int createdSaleId = 0;

            if (booking.Status == BookingStatus.Close)
            {
                var b_products = _bookingProductService.All()
                                 .Where(bp => bp.BookingId == booking.Id)
                                 .ToList();

                decimal primeCost = 0;

                var sale = new Sale()
                {
                    Date      = DateTime.Now.AddHours(3),
                    ShopId    = user.ShopId.Value,
                    PartnerId = booking.PartnerId,
                    Sum       = booking.Sum,
                    CashSum   = _infoMoneyService.All()
                                .Where(x => x.BookingId == booking.Id && x.PaymentType == PaymentType.Cash)
                                .Sum(x => x.Sum),
                    CashlessSum = _infoMoneyService.All()
                                  .Where(x => x.BookingId == booking.Id && x.PaymentType == PaymentType.Cashless)
                                  .Sum(x => x.Sum),
                    Payment    = true,
                    SaleType   = SaleType.Booking,
                    Discount   = booking.Discount,
                    ForRussian = booking.forRussian
                };

                var createdSale = _saleService.Create(sale);

                _infoMoneyService.All()
                .Where(x => x.BookingId == booking.Id)
                .ToList()
                .ForEach(x => {
                    x.SaleId = sale.Id;
                    _infoMoneyService.Update(x);
                });

                _saleInformationService.Create(new SaleInformation
                {
                    SaleId   = createdSale.Id,
                    SaleType = SaleType.Booking
                });

                foreach (var p in b_products)
                {
                    _saleProductService.Create(new SaleProduct()
                    {
                        Amount     = p.Amount,
                        ProductId  = p.ProductId,
                        SaleId     = sale.Id,
                        Additional = p.Additional,
                        Cost       = p.Cost
                    });

                    primeCost += _productOperationService.RealizationProduct(_db, p.ProductId, p.Amount, createdSale.Id);
                }

                sale.PrimeCost = primeCost;
                sale.Margin    = sale.Sum - sale.PrimeCost;
                createdSaleId  = createdSale.Id;

                _saleService.Update(createdSale);
            }

            if (createdSaleId != 0)
            {
                var managerId = _postgresContext.BookingManagersOld
                                .FirstOrDefault(x => x.BookingId == id).ManagerId;

                _postgresContext.SaleManagersOld.Add(
                    new SaleManagerOld(managerId, createdSaleId, booking.Date));
                _postgresContext.SaveChanges();
            }

            return(RedirectToAction("CheckPrint", new { saleId = createdSaleId, operationSum = cashSum + cashlessSum }));
        }