示例#1
0
        public IActionResult AddPrice(Price price)
        {
            _price.Add(price);

            var model = new PriceViewModel
            {
                Packages = _package.GetAll().ToList(),
                Terms    = _term.GetAll().OrderBy(term => term.Value).ToList(),
            };

            return(RedirectToAction("Index"));
        }
 private void savePrices(IEnumerable <Product> products)
 {
     foreach (var product in products)
     {
         var prices = _priceRepository.FindSet(x => x.ProductId == product.Id);
         if (!prices.Any() || prices.Last().ProductPrice != product.Price)
         {
             _priceRepository.Add(new Price
             {
                 ProductId        = product.Id,
                 ProductPrice     = product.Price,
                 RegistrationDate = DateTime.Now
             });
         }
     }
     _priceRepository.Save();
 }
示例#3
0
        public async Task <PriceDto> Create(Guid userId, PriceForCreationDto priceForCreationDto)
        {
            var product = await _productRepository.GetById(priceForCreationDto.ProductId);

            if (product == null)
            {
                throw new KeyNotFoundException($"Product with id: {priceForCreationDto.ProductId} not found.");
            }

            var price = new Price()
            {
                Value     = priceForCreationDto.Value,
                DateTime  = priceForCreationDto.DateTime,
                ProductId = priceForCreationDto.ProductId,
                Product   = product,
                CreatedBy = userId
            };

            price = await _priceRepository.Add(price);

            await _priceRepository.CommitAsync();

            return(_mapper.Map <Price, PriceDto>(price));
        }
示例#4
0
        public async Task <ProductViewModel> UpdateProduct(UserProfile user, ProductDetailViewModel product)
        {
            var business = await _businessRepo.GetByIdAsync(user.business_id.Value);

            int prodId;

            try
            {
                var pr = await _prodRepo.GetByIdAsync(product.Id.Value, user.business_id.Value);

                if (pr != null)
                {
                    prodId = pr.id;
                }
                else
                {
                    throw new Exception("Не указан идентификатор товара / неверный идентификатор.");
                }
            }
            catch (Exception)
            {
                throw new Exception("Не указан идентификатор товара / неверный идентификатор.");
            }


            var price = new Price
            {
                prod_id = prodId,
            };

            var prod = new Product
            {
                id          = prodId,
                name        = product.ProdName,
                business_id = user.business_id,
                unit_id     = product.UnitId,
                attr1       = product.VendorCode,
                attr9       = product.Size,
                attr10      = product.Color
            };

            if (!string.IsNullOrEmpty(product.Category))
            {
                prod.folder_id = await _foldersDataService.GetFolderIdByPath(product.Category, business.id);
            }

            try
            {
                _prodRepo.UpdateProduct(prod);
            }
            catch (Exception)
            {
                throw new Exception("Товар не был изменён.");
            }

            var candidatePrice = _priceRepo.GetPriceByProdId(prodId);

            if (candidatePrice != null)
            {
                if (candidatePrice.price != product.Price)
                {
                    price.price = product.Price;

                    try
                    {
                        _priceRepo.Update(price);
                    }
                    catch (Exception)
                    {
                        throw new Exception("Не получилось изменить цену товара.");
                    }
                }
            }
            else
            {
                price.price = product.Price;
                try
                {
                    _priceRepo.Add(price);
                }
                catch (Exception)
                {
                    throw new Exception("Не получилось добавить цену товара.");
                }
            }

            if (product.img != null)
            {
                try
                {
                    var imgDal = await _imgRepo.GetByIdAsync(prodId);

                    if (imgDal != null && !string.IsNullOrEmpty(imgDal.img_url))
                    {
                        var isDeleted = await _dbBase.Delete("/" + business.name + "/" +
                                                             imgDal.prod_id + "." + imgDal.img_name + "." + imgDal.img_type);

                        if (!isDeleted)
                        {
                            throw new Exception("Невозможно заменить картинку.");
                        }
                    }

                    using (var stream = product.img.OpenReadStream())
                    {
                        var bytes    = ReadToEnd(stream);
                        var memory   = new MemoryStream(bytes);
                        var imgParts = product.ImgBase64.Split(".");
                        var imgUrl   = await _dbBase.Upload(memory, "/" + business.name + "/" +
                                                            prodId + "." + product.ProdName + "." + imgParts[imgParts.Length - 1]);

                        imgDal.img_type     = imgParts[imgParts.Length - 1];
                        imgDal.img_url      = imgUrl;
                        imgDal.img_url_temp = MakeTemporary(imgUrl);
                        imgDal.img_name     = product.ProdName;
                        if (!string.IsNullOrEmpty(product.Category))
                        {
                            imgDal.img_path = product.Category;
                        }

                        await _imgRepo.UpdateImage(imgDal);
                    }
                }
                catch (Exception)
                {
                    throw new Exception("Невозможно изменить картинку товара.");
                }
            }

            return(await GetProduct(user, prodId));
        }
示例#5
0
        public async Task <ProductDto> Create(Guid userId, ProductForCreationDto productForCreationDto)
        {
            var vendor = await _vendorRepository.GetById(productForCreationDto.VendorId);

            if (vendor == null)
            {
                throw new KeyNotFoundException($"Vendor with id: {productForCreationDto.VendorId} not found.");
            }

            var category = await _categoryRepository.GetById(productForCreationDto.CategoryId);

            if (category == null)
            {
                throw new KeyNotFoundException($"Category with id: {productForCreationDto.CategoryId} not found.");
            }

            var product = new Product()
            {
                Name              = productForCreationDto.Name,
                Description       = productForCreationDto.Description,
                Code              = productForCreationDto.Code,
                CategoryId        = productForCreationDto.CategoryId,
                VendorId          = productForCreationDto.VendorId,
                Brand             = productForCreationDto.Brand,
                MinimumStock      = productForCreationDto.MinimumStock,
                Stock             = 0,
                UnitOfMeasurement = productForCreationDto.UnitOfMeasurement,
                CreatedBy         = userId
            };

            product = await _productRepository.Add(product);

            //PurchasePrice
            var purchasePrice = new Price()
            {
                Value     = productForCreationDto.PurchasePrice.Value,
                DateTime  = DateTime.Now,
                ProductId = product.Id,
                PriceType = ePriceTypes.PurchasePrice
            };

            await _priceRepository.Add(purchasePrice);

            //SalePrice
            var salePrice = new Price()
            {
                Value     = productForCreationDto.SalePrice.Value,
                DateTime  = DateTime.Now,
                ProductId = product.Id,
                PriceType = ePriceTypes.SalePrice
            };

            await _priceRepository.Add(salePrice);

            await _productRepository.CommitAsync();

            await _priceRepository.CommitAsync();

            var productDto = _mapper.Map <Product, ProductDto>(product);

            productDto.PurchasePrice = _mapper.Map <Price, PriceDto>(purchasePrice);
            productDto.SalePrice     = _mapper.Map <Price, PriceDto>(salePrice);

            return(productDto);
        }
示例#6
0
 public void Add(Price Price)
 {
     _PriceRepository.Add(Price);
 }