public bool SaveGrid() { (dgBatchNrGrid.View as TableView).PostEditor(); if (!dgBatchNrGrid.HasUnsavedData) return true; var data = (dgBatchNrGrid.ItemsSource as IEnumerable<InvSerieBatchClientUserCustom>).ToList(); var deletedRowItems = dgBatchNrGrid.DeletedRows.Select(x => x.DataItem).Cast<InvSerieBatchClientUserCustom>().ToList(); var modifiedItems = data.Where(x => !deletedRowItems.Contains(x)).ToList(); var addedItems = modifiedItems.Where(x => x.RowId == 0 && x.InvItem != null).ToList(); var updatedItems = modifiedItems.Where(x => x.RowId != 0).ToList(); var deletedItems = deletedRowItems.Where(x => x.RowId != 0).ToList(); var result = TaskHelper.RunSync(() => SaveData(ApiHelpers.GetNewApi(api), OrderLine, addedItems, updatedItems, deletedItems)); if (!result.Success) { busyIndicator.IsBusy = false; Uniconta.ClientTools.Util.UtilDisplay.ShowErrorCode(result.ErrorCode); } return result.Success; } public static async Task<ApiResult> SaveData(CrudAPI crudApi, CreditorOrderLineClientUserCustom orderLine, List<InvSerieBatchClientUserCustom> addedItems, List<InvSerieBatchClientUserCustom> updatedItems, List<InvSerieBatchClientUserCustom> deletedItems) { var allBatchNummerLinks = await ApiHelpers.QueryWrapper(crudApi, api => api.Query<DCOrderLineSerieBatch>(orderLine)); // Remove all changed batchnumber links, new links are created below var allChangedRowIds = LinqHelper.SelectMany(updatedItems.Select(y => y.RowId), deletedItems.Select(y => y.RowId)).ToHashSet(); var allLinksToRemove = allBatchNummerLinks.Where(x => allChangedRowIds.Contains(x.InvSerieBatchRowId)); ApiResult result; result = await ApiHelpers.MultiCrudNoRowId(crudApi, null, null, allLinksToRemove); if (!result.Success) return result; foreach (var item in addedItems) item.QtyMarked = item.Qty; result = await ApiHelpers.MultiCrudNoRowId(crudApi, addedItems, updatedItems, deletedItems); if (!result.Success) return result; var newLinks = LinqHelper.SelectMany(addedItems, updatedItems).Select(x => CreateBatchNumberLink<CreditorOrderLineClient>(x, orderLine, GetQuantity(x))); result = await ApiHelpers.MultiCrudNoRowId(crudApi, newLinks, null, null); if (!result.Success) return result; // We need to save updatedItems again to update the correct amount on the batchnummers. result = await ApiHelpers.MultiCrudNoRowId(crudApi, null, updatedItems, null); if (!result.Success) return result; orderLine.CachedBatchNumbers = null; return ApiResult.Ok(); }
789600cookie-checkUniconta Save BatchNumbers