private async Task UpdateCostAndStocks(int productId, int shopId) { var pureOrderStocks = await _orderStockRepo.GetPureOrderStocksByProdId(productId); decimal?multipleSum = 0; decimal?count = 0; foreach (var item in pureOrderStocks) { multipleSum += item.curr_stocks * (item.OrderDetail?.cost ?? 0); count += item.curr_stocks; } decimal?averageCost = 0; if (count != 0) { averageCost = multipleSum / count; } var cost = _costRepo.GetByProdId(productId).FirstOrDefault(); if (cost != null) { cost.value = averageCost; await _costRepo.UpdateCostValueAsync(cost); } else { var c = new Cost { prod_id = productId, value = averageCost }; await _costRepo.AddCostAsync(c); } var stockDal = _stockRepo.GetStockByShopAndProdIds(shopId, productId); var orderStockShop = await _orderStockRepo.GetPureOrderStocksByProdAndShopIds(productId, shopId); if (stockDal != null) { stockDal.count = orderStockShop.Sum(p => p.curr_stocks); await _stockRepo.UpdateValueAsync(stockDal); } else { var stock = new Stock { prod_id = productId, shop_id = shopId, count = count }; await _stockRepo.AddAsync(stock); } }
public async Task <ActionResult <CostDto> > AddCost([FromBody] Cost cost) { var query = await _repository.AddCostAsync(cost); if (query != null) { await _repository.CommitAsync(); } return(query.Entity.Adapt <CostDto>()); }
/// <summary> /// Inserting product /// </summary> /// <param name="user">user info</param> /// <param name="product">product view model</param> public async Task <ProductDetailViewModel> AddProduct(UserProfile user, ProductDetailViewModel product) { if (string.IsNullOrEmpty(product.ProdName)) { throw new Exception("Наименование товара не может быть пустым."); } var business = await _businessRepo.GetByIdAsync(user.business_id.Value); //create product model var prod = new Product { name = product.ProdName, business_id = business.id, unit_id = product.UnitId, attr1 = product.VendorCode, attr9 = product.Size, attr10 = product.Color }; //add price link prod.Prices.Add(new Price { price = product.Price }); int pId = 0; try { //await foldersDataService.AddFoldersByPath(product.Category, business.id); prod.folder_id = await _foldersDataService.GetFolderIdByPath(product.Category, business.id); //add with repo pId = _prodRepo.AddProduct(prod); } catch (Exception) { throw new Exception("Не получилось добавить товар в базу. Проверьте правильность заполнения полей."); } if (!string.IsNullOrEmpty(product.ImgBase64)) { try { 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 + "/" + pId + "." + product.ProdName + "." + imgParts[imgParts.Length - 1]); var img = new Image { img_url = imgUrl, prod_id = pId, img_name = product.ProdName, img_type = imgParts[imgParts.Length - 1], img_url_temp = MakeTemporary(imgUrl), img_path = product.Category }; _imgRepo.Add(img); } } catch (Exception) { throw new Exception("Не получилось добавить картинку товара. Попробуйте снова."); } } var dt = DateTime.Now; if (product.Cost.HasValue && product.Cost < 0) { throw new Exception("Себестоимость должна быть > 0."); } if (product.Cost.HasValue && product.Cost >= 0) { if (product.Stocks != null && product.Stocks.Any()) { foreach (var s in product.Stocks) { var order = new Order { isOrder = true, report_date = dt, shop_id = s.ShopId, OrderDetails = new List <OrderDetail> { new OrderDetail { prod_id = pId, cost = product.Cost.Value, count = s.Stock } } }; try { var orderId = await _ordersRepo.AddOrderAsync(order); var orderDal = await _ordersRepo.GetByIdWithMultiAsync(orderId); await _strategy.UpdateAverageCost(Direction.Order, orderDal); } catch (Exception) { throw new Exception("Не удалось добавить остаток на склад."); } } } else { var cost = new Cost { prod_id = pId, value = product.Cost }; await _costRepo.AddCostAsync(cost); } } product.Id = pId; return(product); }