new public async Task <Guid> CreateAsync(WarehousingForCreationDto creationDto) { var newWarehousing = new WarehousingEntity(); foreach (PropertyInfo propertyInfo in creationDto.GetType().GetProperties()) { if (newWarehousing.GetType().GetProperty(propertyInfo.Name) != null && propertyInfo.Name != "ProductList" && propertyInfo.Name != "SupplierBillList") { newWarehousing.GetType().GetProperty(propertyInfo.Name).SetValue(newWarehousing, propertyInfo.GetValue(creationDto, null)); } } newWarehousing.IsActive = true; newWarehousing.SupplierBillList = JsonConvert.SerializeObject(creationDto.SupplierBillList); newWarehousing.CreatedDateTime = DateTime.Now; var user = await _userManager.GetUserAsync(_httpContextAccessor.HttpContext.User); newWarehousing.CreatedUserId = user.Id; newWarehousing.ProductList = JsonConvert.SerializeObject(creationDto.ProductList); newWarehousing.SupplierBillList = JsonConvert.SerializeObject(creationDto.SupplierBillList); double productMoney = 0; foreach (var product in creationDto.ProductList) { // calculate product money productMoney += product.InputAmount * product.InputPrice; } newWarehousing.ProductMoney = productMoney; newWarehousing.SummaryMoney = newWarehousing.ProductMoney + newWarehousing.TaxMoney; newWarehousing.DebtMoney = newWarehousing.SummaryMoney - creationDto.PaymentMoney; // generate the code bool isDuplicated = false; do { string lastestCode = _entity.Max(w => w.Code); newWarehousing.Code = CodeGeneratorHelper.GetGeneratedCode(CONSTANT.WAREHOUSING_PREFIX, lastestCode, CONSTANT.GENERATED_NUMBER_LENGTH); isDuplicated = await IsDuplicatedCode(newWarehousing.Code); } while (isDuplicated); await _entity.AddAsync(newWarehousing); // update inventory of ProductStorages foreach (var product in creationDto.ProductList) { var productStorage = await _context.ProductStorages.SingleOrDefaultAsync(p => p.ProductId == product.Id && p.StorageId == newWarehousing.StorageId); _addCapitalTracking(product, newWarehousing.StorageId, newWarehousing.Id, productStorage); _updateInventoryDetail(product, null, productStorage); } var created = await _context.SaveChangesAsync(); if (created < 1) { throw new InvalidOperationException("Database context could not create Warehousing."); } return(newWarehousing.Id); }
new public async Task <Guid> CreateAsync(BillForCreationDto creationDto) { var customer = await _customerEntity.FirstOrDefaultAsync(x => x.Id == creationDto.CustomerId); if (customer == null) { throw new Exception("Can not find any customer with this id"); } if (creationDto.UsedPoints > customer.AccumulationPoint) { throw new Exception("UsedPoints must be less or equal AccumulationPoint"); } var newBill = new BillEntity(); foreach (PropertyInfo propertyInfo in creationDto.GetType().GetProperties()) { if (newBill.GetType().GetProperty(propertyInfo.Name) != null && propertyInfo.Name != "ProductList") { newBill.GetType().GetProperty(propertyInfo.Name).SetValue(newBill, propertyInfo.GetValue(creationDto, null)); } } var settings = _context.Settings.ToList(); var defaultSetting = settings[0]; // we have a lot of data to save! newBill.PointMoney = defaultSetting.PointToMoney * creationDto.UsedPoints; newBill.PointEarning = Math.Round(creationDto.TotalMoney / defaultSetting.MoneyToPoint, 1); newBill.CreatedDateTime = DateTimeHelper.GetVietnamNow(); newBill.IsActive = true; newBill.ProductList = JsonConvert.SerializeObject(creationDto.ProductList); var user = await _userManager.GetUserAsync(_httpContextAccessor.HttpContext.User); newBill.UserId = user.Id; // generate the code bool isDuplicated = false; do { string lastestCode = _entity.Max(w => w.Code); newBill.Code = CodeGeneratorHelper.GetGeneratedCode(CONSTANT.BILL_PREFIX, lastestCode, CONSTANT.GENERATED_NUMBER_LENGTH); isDuplicated = await _isDuplicatedCode(newBill.Code); } while (isDuplicated); await _entity.AddAsync(newBill); // update accumulation points customer.AccumulationPoint += newBill.PointEarning - creationDto.UsedPoints; customer.AccumulationPoint = Math.Round(customer.AccumulationPoint, 1); if (creationDto.IsUpdatedCustomerShipping) { customer.ShipAddress = newBill.ShipAddress; customer.ShipContactName = newBill.ShipContactName; customer.ShipPhone = newBill.ShipPhone; customer.CompanyName = newBill.CompanyName; customer.CompanyAddress = newBill.CompanyAddress; customer.CompanyTaxCode = newBill.CompanyTaxCode; } _customerEntity.Update(customer); // update inventory for eact saled product foreach (ProductForBillCreationDto product in creationDto.ProductList) { if (product.IsService) // if product is service, we need not update anymore { continue; } var productStorage = _context.ProductStorages.FirstOrDefault(p => p.ProductId == product.Id && p.StorageId == creationDto.StorageId); // if has a ProductStorage, we update inventory number. if (productStorage != null) { if (product.Amount > productStorage.Inventory && !defaultSetting.IsAllowNegativeInventoryBill) { throw new Exception("amount_overcomes_inventory"); } productStorage.Inventory -= product.Amount; // update detail output amount _addOrUpdateProductProductionDate(productStorage, product, true); CapitalPriceTrackingDto capitalPriceTracking = new CapitalPriceTrackingDto { BillId = newBill.Id, Amount = product.Amount, 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); } else { throw new Exception("ProductStorage is not exist"); } } var created = await _context.SaveChangesAsync(); if (created < 1) { throw new InvalidOperationException("Database context could not create data."); } return(newBill.Id); }