private void _removeCapitalTrackingAndUpdateCapitalPriceInventory(ProductStorageEntity productStorage, ProductForBillCreationDto product, Guid billId) { // update capital price with average if (productStorage.CapitalPriceTrackings == null) { throw new Exception("CapitalPriceTrackings is NULL with ProductStorage.Id=" + productStorage.Id); } var capitalPriceTrackings = JsonConvert.DeserializeObject <List <CapitalPriceTrackingDto> >(productStorage.CapitalPriceTrackings); var startedIndex = capitalPriceTrackings.FindIndex(c => c.BillId == billId); capitalPriceTrackings.RemoveAt(startedIndex); for (int i = startedIndex; i < capitalPriceTrackings.Count; i++) { capitalPriceTrackings[i].Inventory = capitalPriceTrackings[i - 1].Inventory + capitalPriceTrackings[i].Amount; capitalPriceTrackings[i].CapitalPrice = ProductStorageHelper.CalculateCapitalPrice( capitalPriceTrackings[i - 1].Inventory, capitalPriceTrackings[i - 1].CapitalPrice, capitalPriceTrackings[i].Amount, capitalPriceTrackings[i].InputPrice ); } productStorage.CapitalPriceTrackings = JsonConvert.SerializeObject(capitalPriceTrackings); productStorage.CapitalPrice = capitalPriceTrackings[capitalPriceTrackings.Count - 1].CapitalPrice; productStorage.Inventory = capitalPriceTrackings[capitalPriceTrackings.Count - 1].Inventory; _context.ProductStorages.Update(productStorage); }
private void _updateCapitalTrackingCapitalPriceAndInventory(ProductStorageEntity productStorage, ProductForBillCreationDto product, double newAmount, Guid billId) { // update capital price with average if (productStorage.CapitalPriceTrackings == null) { throw new Exception("CapitalPriceTrackings is NULL with ProductStorage.Id=" + productStorage.Id); } var capitalPriceTrackings = JsonConvert.DeserializeObject <List <CapitalPriceTrackingDto> >(productStorage.CapitalPriceTrackings); var startedIndex = capitalPriceTrackings.FindIndex(c => c.BillId == billId); if (startedIndex < 0) // product is new on bill, we need to create a new tracking { var newCapitalPriceTrackingDto = new CapitalPriceTrackingDto() { BillId = billId, Amount = newAmount, CapitalPrice = productStorage.CapitalPrice, Inventory = productStorage.Inventory - newAmount }; capitalPriceTrackings.Add(newCapitalPriceTrackingDto); } else { capitalPriceTrackings[startedIndex].Amount = newAmount; for (int i = startedIndex; i < capitalPriceTrackings.Count; i++) { if (capitalPriceTrackings[i].BillId != Guid.Empty) // this is a bill. we need to decrease the invetory { capitalPriceTrackings[i].Inventory = capitalPriceTrackings[i - 1].Inventory - capitalPriceTrackings[i].Amount; } else { capitalPriceTrackings[i].Inventory = capitalPriceTrackings[i - 1].Inventory + capitalPriceTrackings[i].Amount; } if (capitalPriceTrackings[i].BillId == Guid.Empty) // this is not a bill. we only update capital price for warehousing and inventory { capitalPriceTrackings[i].CapitalPrice = ProductStorageHelper.CalculateCapitalPrice( capitalPriceTrackings[i - 1].Inventory, capitalPriceTrackings[i - 1].CapitalPrice, capitalPriceTrackings[i].Amount, capitalPriceTrackings[i].InputPrice ); } else { capitalPriceTrackings[i].CapitalPrice = capitalPriceTrackings[i - 1].CapitalPrice; } } } productStorage.CapitalPriceTrackings = JsonConvert.SerializeObject(capitalPriceTrackings); productStorage.CapitalPrice = capitalPriceTrackings[capitalPriceTrackings.Count - 1].CapitalPrice; productStorage.Inventory = capitalPriceTrackings[capitalPriceTrackings.Count - 1].Inventory; _context.ProductStorages.Update(productStorage); }
new public async Task <Guid> CreateAsync(StorageDto storageDto) { StorageEntity newStorage = new StorageEntity() { Name = storageDto.Name, Address = storageDto.Address }; await _entity.AddAsync(newStorage); // we need to generate product storages for all product var products = await _context.Products.ToListAsync(); foreach (var product in products) { var productStorage = new ProductStorageEntity() { ProductId = product.Id, StorageId = newStorage.Id, ProductProductionDateList = null, Inventory = 0, CapitalPrice = 0, PlacePosition = null, CapitalPriceTrackings = null }; await _context.AddAsync(productStorage); } // we need to create user storage for all owners var owners = await _userRepository.GetUsersWithRoleNameAsync(CONSTANT.ROLE_OWNER); foreach (var owner in owners) { var userStorage = new UserStorageEntity() { StorageId = newStorage.Id, UserId = owner.Id }; _context.UserStorages.Add(userStorage); } ; var created = await _context.SaveChangesAsync(); if (created < 1) { throw new InvalidOperationException("Database context could not create data."); } return(newStorage.Id); }
new public async Task <Guid> CreateAsync(ProductForCreationDto creationDto) { var newProduct = new ProductEntity(); var existedProduct = _entity.FirstOrDefault(p => p.Code == creationDto.Code); if (existedProduct != null) { throw new InvalidOperationException("Product has Code which is existed on database"); } foreach (PropertyInfo propertyInfo in creationDto.GetType().GetProperties()) { if (newProduct.GetType().GetProperty(propertyInfo.Name) != null) { newProduct.GetType().GetProperty(propertyInfo.Name).SetValue(newProduct, propertyInfo.GetValue(creationDto, null)); } } newProduct.CreatedDate = DateTime.Now; await _entity.AddAsync(newProduct); //start add productstorage var storages = _context.Storages.ToArray(); foreach (var storage in storages) { var productStorage = new ProductStorageEntity(); productStorage.ProductId = newProduct.Id; productStorage.StorageId = storage.Id; productStorage.Inventory = 0; await _context.ProductStorages.AddAsync(productStorage); } //end add productstorage var created = await _context.SaveChangesAsync(); if (created < 1) { throw new InvalidOperationException("Database context could not create data."); } return(newProduct.Id); }
private void _addOrUpdateProductProductionDateWithAmount(ProductStorageEntity productStorage, ProductForBillCreationDto newProduct, ProductForBillCreationDto oldProduct) { if (newProduct.DetailAmountList != null && newProduct.DetailAmountList.Count > 0) { var oldProductProductionDateList = JsonConvert.DeserializeObject <List <ProductProductionDateDto> >(productStorage.ProductProductionDateList); foreach (var newProductDetailAmount in newProduct.DetailAmountList) { if (newProductDetailAmount.Amount <= 0) { continue; } var oldProductProductionDate = oldProductProductionDateList.SingleOrDefault(p => p.ProductionWeekYear == newProductDetailAmount.ProductionWeekYear); if (oldProductProductionDate == null) { // add new product production date var newProductProductionDate = new ProductProductionDateDto() { ProductionWeekYear = newProductDetailAmount.ProductionWeekYear, ProductionDate = DateTimeHelper.ConvertWeekYearToDateTime(newProductDetailAmount.ProductionWeekYear), Inventory = 0 - newProductDetailAmount.Amount }; oldProductProductionDateList.Add(newProductProductionDate); } else { double oldAmount = 0; if (oldProduct != null) { var oldDetailAmount = oldProduct.DetailAmountList.SingleOrDefault(d => d.ProductionWeekYear == newProductDetailAmount.ProductionWeekYear); oldAmount = oldDetailAmount.Amount; } oldProductProductionDate.Inventory += oldAmount - newProductDetailAmount.Amount; } productStorage.ProductProductionDateList = JsonConvert.SerializeObject(oldProductProductionDateList); } _context.ProductStorages.Update(productStorage); } }
private void _addOrUpdateProductProductionDate(ProductStorageEntity productStorage, ProductForBillCreationDto product, bool isDecreaseInventory) { if (product.DetailAmountList != null && product.DetailAmountList.Count > 0) { var productProductionDateList = JsonConvert.DeserializeObject <List <ProductProductionDateDto> >(productStorage.ProductProductionDateList); foreach (var detailAmount in product.DetailAmountList) { var productProductionDate = productProductionDateList.SingleOrDefault(p => p.ProductionWeekYear == detailAmount.ProductionWeekYear); if (productProductionDate == null) { // add new product production date var newProductProductionDate = new ProductProductionDateDto() { ProductionWeekYear = detailAmount.ProductionWeekYear, ProductionDate = DateTimeHelper.ConvertWeekYearToDateTime(detailAmount.ProductionWeekYear), Inventory = 0 - detailAmount.Amount }; productProductionDateList.Add(newProductProductionDate); } else { if (isDecreaseInventory) { productProductionDate.Inventory -= detailAmount.Amount; } else { productProductionDate.Inventory += detailAmount.Amount; } } productStorage.ProductProductionDateList = JsonConvert.SerializeObject(productProductionDateList); } _context.ProductStorages.Update(productStorage); } }
private void _updateInventoryDetail(ProductForWareshousingCreationDto editedProduct, ProductForWareshousingCreationDto oldProduct, ProductStorageEntity productStorage) { // update detail inventory for each product var oldProductProductionList = (productStorage.ProductProductionDateList == null) ? new List <ProductProductionDateDto>() : JsonConvert.DeserializeObject <List <ProductProductionDateDto> >(productStorage.ProductProductionDateList); var detailInputAmountList = editedProduct.DetailInputAmountList; if (detailInputAmountList != null && detailInputAmountList.Count > 0) { foreach (var detailInputAmount in detailInputAmountList) { if (detailInputAmount.InputAmount > editedProduct.InputAmount) { throw new Exception("Detail InputAmount is greater than product InputAmount"); } var oldProductProduction = oldProductProductionList.SingleOrDefault(p => p.ProductionWeekYear == detailInputAmount.ProductionWeekYear); if (oldProductProduction != null) { if (oldProduct == null) { oldProductProduction.Inventory += (detailInputAmount.InputAmount); } else { var beforeAddedProductDetailInputAmount = oldProduct.DetailInputAmountList.SingleOrDefault(p => p.ProductionWeekYear == detailInputAmount.ProductionWeekYear); double beforeInputAmount = (beforeAddedProductDetailInputAmount == null) ? 0 : beforeAddedProductDetailInputAmount.InputAmount; oldProductProduction.Inventory += (detailInputAmount.InputAmount - beforeInputAmount); } } else { oldProductProductionList.Add(new ProductProductionDateDto() { ProductionWeekYear = detailInputAmount.ProductionWeekYear, ProductionDate = DateTimeHelper.ConvertWeekYearToDateTime(detailInputAmount.ProductionWeekYear), Inventory = detailInputAmount.InputAmount }); } } productStorage.ProductProductionDateList = JsonConvert.SerializeObject(oldProductProductionList); //_context.ProductStorages.Update(productStorage); } }
private void _addCapitalTracking(ProductForWareshousingCreationDto product, Guid storageId, Guid warehousingId, ProductStorageEntity productStorage) { // create capital price tracking productStorage.CapitalPrice = ProductStorageHelper.CalculateCapitalPrice( productStorage.Inventory, productStorage.CapitalPrice, product.InputAmount, product.InputPrice ); productStorage.Inventory += product.InputAmount; CapitalPriceTrackingDto capitalPriceTracking = new CapitalPriceTrackingDto { WarehousingId = warehousingId, Amount = product.InputAmount, InputPrice = product.InputPrice, CapitalPrice = productStorage.CapitalPrice, Inventory = productStorage.Inventory }; productStorage.CapitalPriceTrackings = productStorage.CapitalPriceTrackings ?? "[]"; var capitalPriceTrackings = JsonConvert.DeserializeObject <List <CapitalPriceTrackingDto> >(productStorage.CapitalPriceTrackings); capitalPriceTrackings.Add(capitalPriceTracking); productStorage.CapitalPriceTrackings = JsonConvert.SerializeObject(capitalPriceTrackings); _context.ProductStorages.Update(productStorage); }
new public async Task <Guid> CreateAsync(ProductForCreationDto creationDto) { var newProduct = new ProductEntity(); var existedProduct = await _entity.SingleOrDefaultAsync(p => p.Code == creationDto.Code); if (existedProduct != null) { throw new InvalidOperationException("Product has Code which is existed on database"); } foreach (PropertyInfo propertyInfo in creationDto.GetType().GetProperties()) { if (newProduct.GetType().GetProperty(propertyInfo.Name) != null && propertyInfo.Name != "UnitConverterList") { newProduct.GetType().GetProperty(propertyInfo.Name).SetValue(newProduct, propertyInfo.GetValue(creationDto, null)); } } newProduct.UnitConverterList = (creationDto.UnitConverterList == null) ? null : JsonConvert.SerializeObject(creationDto.UnitConverterList); newProduct.UpdatePriceDate = DateTime.Now; await _entity.AddAsync(newProduct); var storages = _context.Storages.ToArray(); foreach (var storage in storages) { var productStorage = new ProductStorageEntity { ProductId = newProduct.Id, StorageId = storage.Id, Inventory = 0 }; if (storage.Id == creationDto.CurrentStorageId) { productStorage.Inventory = creationDto.Inventory; productStorage.PlacePosition = creationDto.PlacePosition; productStorage.CapitalPrice = creationDto.CapitalPrice; if (creationDto.ProductProductionDateList != null) { foreach (var productProductionDate in creationDto.ProductProductionDateList) { if (productProductionDate.ProductionWeekYear != null) { DateTime productionDate = DateTimeHelper.ConvertWeekYearToDateTime(productProductionDate.ProductionWeekYear); productProductionDate.ProductionDate = productionDate; } } productStorage.ProductProductionDateList = JsonConvert.SerializeObject(creationDto.ProductProductionDateList); } // create tracking capital price CapitalPriceTrackingDto capitalPriceTracking = new CapitalPriceTrackingDto { Amount = creationDto.Inventory, InputPrice = creationDto.CapitalPrice, CapitalPrice = creationDto.CapitalPrice, Inventory = creationDto.Inventory }; var capitalPriceTrackings = new List <CapitalPriceTrackingDto> { capitalPriceTracking }; productStorage.CapitalPriceTrackings = JsonConvert.SerializeObject(capitalPriceTrackings); } await _context.ProductStorages.AddAsync(productStorage); } var created = await _context.SaveChangesAsync(); if (created < 1) { throw new InvalidOperationException("Database context could not create data."); } return(newProduct.Id); }