Esempio n. 1
0
        public IActionResult SaleFromStock([FromBody] SaleFromStockVM json)
        {
            try
            {
                var supplierId = _db.Suppliers.FirstOrDefault(x => x.Title == json.Supplier)?.Id
                                 ?? throw new Exception("Не указан поставщик");

                var managerId = _postgresContext.Managers.FirstOrDefault(x => x.Name == json.Manager)?.Id
                                ?? throw new Exception("Не указан менеджер");

                SaleCreateVM saleCreate = new SaleCreateVM()
                {
                    UserId            = json.UserId,
                    Discount          = json.Discount,
                    AdditionalComment = json.AdditionalComment,
                    Comment           = json.Comment,
                    Sum         = json.Sum,
                    CashSum     = json.Cash,
                    CashlessSum = json.Cashless,
                    Products    = json.Products,
                    PartnerId   = json.Buyer == "Обычный покупатель"
                        ? null
                        : _partnerService.All().First(p => p.Title == json.Buyer)?.Id,
                    SaleType   = SaleType.SaleFromStock,
                    ForRussian = json.ForRussian
                };

                var createdSale = _saleService.CreatePostPayment(_db, saleCreate, json.UserId);
                var shop        = _shopService.ShopByUserId(_db, json.UserId);

                _db.SaleInformations.Add(new SaleInformation()
                {
                    Sale = createdSale,
                    MoneyWorkerForIncomeId         = shop.Id,
                    MoneyWorkerForCashlessIncomeId = json.MoneyWorkerIdForCashlessIncome == 0
                        ? null
                        : json.MoneyWorkerIdForCashlessIncome,
                    SaleType = SaleType.SaleFromStock
                });

                _db.SaveChanges();


                var saleFromStockOld = new SaleFromStockOld()
                {
                    SaleId     = createdSale.Id,
                    SupplierId = supplierId,
                    Products   = json.Products.Select(x =>
                                                      new SoldProductFromStockOld()
                    {
                        ProcurementCost = x.ProcurementCost,
                        ProductId       = x.Id
                    }).ToList()
                };


                _postgresContext.SalesFromStockOld.Add(saleFromStockOld);

                _postgresContext.SaleManagersOld.Add(
                    new SaleManagerOld(managerId, createdSale.Id, DateTime.Now.AddHours(3)));

                _postgresContext.SaveChanges();


                return(RedirectToAction("CheckPrint", new { saleId = createdSale.Id, operationSum = json.Cash + json.Cashless }));
            }
            catch (Exception e)
            {
                return(BadRequest(new { message = e.Message }));
            }
        }