public void Expense(PostgresContext postgresContext, int moneyWorkerId, decimal sum, PaymentType paymentType,
                            int categoryId, string comment, int forId)
        {
            var createdInfoMoney = _infoMoneyService.Create(new InfoMoney()
            {
                MoneyWorkerId      = moneyWorkerId,
                Sum                = -sum,
                MoneyOperationType = MoneyOperationType.Expense,
                PaymentType        = paymentType,
                Comment            = comment
            });

            var expense = _expenseService.Create(new Expense()
            {
                InfoMoneyId       = createdInfoMoney.Id,
                ExpenseCategoryId = categoryId,
            });

            postgresContext.ExpensesOld.Add(new ExpenseOld(expense.Id, forId));
            postgresContext.SaveChanges();
        }
Example #2
0
        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 }));
        }
Example #3
0
        public Booking Booking(BookingVM booking, int userId)
        {
            var shopId = _context.Users.First(x => x.Id == userId).ShopId;

            var productsIds = booking.Products.Select(x => x.Id);
            var products    = _context.Products.Where(x => productsIds.Contains(x.Id));

            Booking newBooking = new Booking()
            {
                Date        = DateTime.Now.AddHours(3),
                ShopId      = shopId,
                UserId      = userId,
                CashSum     = booking.CashSum,
                CashlessSum = booking.CashlessSum,
                Sum         = booking.Sum,
                Discount    = booking.Discount,
                Debt        = booking.Sum - booking.CashSum - booking.CashlessSum,
                Pay         = booking.CashSum + booking.CashlessSum,
                Status      = BookingStatus.Open,
                PartnerId   = booking.Buyer == "Обычный покупатель"
                    ? null
                    : _context.Partners.First(p => p.Title == booking.Buyer)?.Id,
                forRussian = booking.forRussian
            };

            var createdBooking = _bookingService.Create(newBooking);

            foreach (var product in booking.Products)
            {
                _bookingProductService.Create(new BookingProduct()
                {
                    Amount     = product.Amount,
                    BookingId  = createdBooking.Id,
                    ProductId  = product.Id,
                    Additional = product.Additional,
                    Cost       = product.Cost
                });
            }

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

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

            return(createdBooking);
        }