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 _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 _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 _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); }