示例#1
0
        public async Task <IActionResult> NewSale(SaleCreateViewModel model)
        {
            var session = HttpContext.Session;

            if (ModelState.IsValid)
            {
                Sale sale = new Sale()
                {
                    SaleDate   = DateTime.Now,
                    SaleId     = model.SaleId,
                    CustomerId = model.CustomerId,
                    EmployeeId = model.EmployeeId
                };
                await iSaleService.CreateAsync(sale);

                session.SetInt32("Current CustomerID", (int)sale.CustomerId);
                session.SetInt32("Current SaleID", sale.SaleId);

                return(RedirectToAction("Create", "SaleItem"));
            }

            ViewBag.CustomerID = new SelectList(iCustomerService.GetCustomers(), "CustomerId", "FullName", model.CustomerId);
            ViewBag.EmployeeID = new SelectList(iEmployeeService.GetEmployees().Where(x => !x.Designation.Contains("Speaker")), "Id", "DropdownStr", model.EmployeeId);
            return(View(model));
        }
示例#2
0
        public async Task <IActionResult> Create(SaleDto saleDto)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var sale = await saleService.ConvertFromDtoAsync(saleDto);

                    var created = await saleService.CreateAsync(sale);

                    foreach (var saleProduct in sale.SaleProduct)
                    {
                        await productService.DecreaseStock(saleProduct.ProductId, saleProduct.Quantity);
                    }

                    var Dto = saleService.ConvertToDto(created);
                    return(CreatedAtAction(nameof(GetSaleById), new { id = Dto.SaleId }, Dto));
                }
                catch (Exception)
                {
                    return(BadRequest());
                }
            }
            return(BadRequest());
        }
示例#3
0
        public async Task <IActionResult> Create(string ids, int?locationId, string locationName)
        {
            return(await TryGetActionResultAsync(async() =>
            {
                var model = new SaleCreateUpdateViewModel {
                    AllowCreation = AllowCreation
                };

                if (!LocationId.HasValue && !locationId.HasValue)
                {
                    model.AllowChangeLocation = true;
                    return View(model);
                }

                var result = await _saleService.CreateAsync(new SaleDto
                {
                    LocationID = locationId ?? LocationId.Value,
                    UserID = UserId,
                    Date = DateTime.Now
                }, UserId);

                if (result.Status != ServiceResponseStatus.Success)
                {
                    TempData["Error"] = "Не удалось оформить продажу.";
                    return RedirectToAction("Index");
                }

                if (!string.IsNullOrWhiteSpace(ids))
                {
                    var goodsIds = ids.Split(',').Select(id => Convert.ToInt32(id));
                    var goodsResult = await _saleService.AddGoodsAsync(goodsIds, null, result.Result, UserId);
                    if (goodsResult.Status != ServiceResponseStatus.Success)
                    {
                        await _saleService.DeleteAsync(result.Result, UserId);

                        TempData["Error"] = "Не удалось оформить продажу.";
                        return RedirectToAction("Index");
                    }
                }

                TempData["Success"] = "Продажа оформлена. Для отмены нажмите \"Отменить продажу\". Для подтверждения оформления нажмите \"Подтвердить\".";

                model.ID = result.Result;
                model.LocationID = locationId ?? LocationId.Value;
                model.LocationName = locationName ?? LocationName;
                model.AllowChangeLocation = false;

                return View(model);
            }, OnFault));
        }
示例#4
0
        public async Task <IActionResult> Create(SaleFormViewModel saleFormViewModel)
        {
            if (ModelState.IsValid)
            {
                List <SaleProduct> saleProductList = new List <SaleProduct>();
                double             totalValue      = 0;

                foreach (var saleProduct in saleFormViewModel.SaleProducts)
                {
                    saleProductList.Add(saleProduct);

                    var getProduct = await productService.GetByIdAsync(saleProduct.ProductId);

                    var unitPrice = getProduct.UnitPrice;

                    totalValue += saleProduct.Quantity * unitPrice;
                }

                Sale sale = new Sale()
                {
                    SaleProduct = saleProductList,
                    ClientName  = saleFormViewModel.Sale.ClientName,
                    SaleDate    = saleFormViewModel.Sale.SaleDate,
                    TotalValue  = totalValue
                };

                var response = await saleService.CreateAsync(sale);

                if (response == null)
                {
                    return(BadRequest());
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View());
        }