public void SaveMainRow() { if (SelectedMainRow != null) { var valiationCollection = new List <ValidationResult>(); var isvalid = Validator.TryValidateObject(SelectedMainRow, new ValidationContext(SelectedMainRow, null, null), valiationCollection, true); if (isvalid) { var saveRow = new TblBankStatHeader() { DocDate = DateTime.Now, CreationDate = DateTime.Now, }; saveRow.InjectFrom(SelectedMainRow); saveRow.TblBankStatDetails = new ObservableCollection <TblBankStatDetail>(); foreach (var item in SelectedMainRow.TblBankStatDetails) { var detailTemp = new TblBankStatDetail(); detailTemp.InjectFrom(item); saveRow.TblBankStatDetails.Add(detailTemp); } var mainRowIndex = MainRowList.IndexOf(SelectedMainRow); if (mainRowIndex < 0) { MainRowList.Insert(mainRowIndex + 1, SelectedMainRow); mainRowIndex++; } BankStatClient.UpdateOrInsertBankStatHeaderAsync(saveRow, mainRowIndex, LoggedUserInfo.Iserial, LoggedUserInfo.DatabasEname); } } }
private void InsertImportedDetail(ObservableCollection <ImportedBankStatement> importedList) { TblBankStatHeader headerRow = new TblBankStatHeader(); headerRow.InjectFrom(SelectedMainRow); if (ValidData()) { var temp = new ObservableCollection <ImportedBankStatement>(importedList.Skip(0).Take(0)); BankStatClient.InsertImportedItemsAsync(headerRow, temp, LoggedUserInfo.DatabasEname);// BankStat Header RemainningImportList = new ObservableCollection <ImportedBankStatement>( importedList.Skip(0)); } }
public BankStatementViewModel() : base(PermissionItemName.BankStatement) { if (!DesignerProperties.IsInDesignTool) { ApproveBankStat = new RelayCommand(() => { if (SaveCommand.CanExecute(null)) { SaveCommand.Execute(null); } SelectedMainRow.Approved = true; SelectedMainRow.ApproveDate = DateTime.Now; SelectedMainRow.ApprovedBy = LoggedUserInfo.Iserial; if (SaveCommand.CanExecute(null)) { SaveCommand.Execute(null); } if (SelectedMainRow.Approved)//كده نفذ فهعمل جديد { if (NewCommand.CanExecute(null)) { NewCommand.Execute(null); } } }, () => CheckCanApprove()); DeleteBankStatDetail = new RelayCommand <object>((o) => { if (((KeyEventArgs)(o)).Key == Key.Delete) { if (SelectedMainRow.Iserial <= 0 || SelectedDetailRow.Iserial <= 0) { SelectedMainRow.TblBankStatDetails.Remove(SelectedDetailRow); if (SelectedMainRow.TblBankStatDetails.Count == 0) { AddNewDetailRow(false); } } else { DeleteDetailRow(); } } RaisePropertyChanged(nameof(IsHeaderHasDetails)); }, (o) => { return(SelectedMainRow != null && !SelectedMainRow.Approved); }); LoadingDetailRows = new RelayCommand <object>((o) => { var e = o as DataGridRowEventArgs; if (SelectedMainRow.TblBankStatDetails.Count < PageSize) { return; } if (SelectedMainRow.TblBankStatDetails.Count - 2 < e.Row.GetIndex() && !Loading) { GetDetailData(); } }); NewDetail = new RelayCommand <object>((o) => { var e = o as SelectionChangedEventArgs; if (((KeyEventArgs)(o)).Key == Key.Down) { AddNewDetailRow(false); } RaisePropertyChanged(nameof(IsHeaderHasDetails)); }); this.PremCompleted += (s, sv) => { if (this.CustomePermissions.SingleOrDefault(x => x.Code == "BankStatApprove") != null) { CanApprove = true; } }; this.GetCustomePermissions(PermissionItemName.BankStatement.ToString()); MainRowList = new ObservableCollection <BankStatHeader>(); AddNewMainRow(false); BankStatClient.GetBankStatHeaderCompleted += (s, sv) => { foreach (var row in sv.Result) { var newrow = new BankStatHeader(); newrow.InjectFrom(row); MainRowList.Add(newrow); } Loading = false; FullCount = sv.fullCount; if (SearchWindow != null) { SearchWindow.FullCount = sv.fullCount; SearchWindow.Loading = false; } if (FullCount == 0 && MainRowList.Count == 0) { AddNewMainRow(false); } }; BankStatClient.GetBankStatDetailCompleted += (s, sv) => { foreach (var row in sv.Result) { var newrow = new BankStatDetail(); newrow.InjectFrom(row); //newrow.TblBankTransactionType1 = BankTransactionTypeList.FirstOrDefault(btt => btt.Iserial == newrow.TblBankTransactionType); SelectedMainRow.TblBankStatDetails.Add(newrow); } if (!SelectedMainRow.TblBankStatDetails.Any()) { AddNewDetailRow(false); } Loading = false; RaisePropertyChanged(nameof(Total)); RaisePropertyChanged(nameof(IsHeaderHasDetails)); }; BankStatClient.UpdateOrInsertBankStatHeaderCompleted += (s, x) => { BankStatHeader savedRow = null; if (x.outindex >= 0) { savedRow = MainRowList.ElementAt(x.outindex); } if (savedRow != null) { savedRow.InjectFrom(x.Result); savedRow.TblBank1 = BankList.FirstOrDefault(b => b.Iserial == savedRow.TblBank); savedRow.TblCurrency1 = CurrencyList.FirstOrDefault(c => c.Iserial == savedRow.TblCurrency); savedRow.TblBankStatDetails.Clear(); foreach (var item in x.Result.TblBankStatDetails) { var detailTemp = new BankStatDetail(); detailTemp.InjectFrom(item); savedRow.TblBankStatDetails.Add(detailTemp); } } RaisePropertyChanged(nameof(IsHeaderHasDetails)); DeleteCommand.RaiseCanExecuteChanged(); ApproveBankStat.RaiseCanExecuteChanged(); DeleteBankStatDetail.RaiseCanExecuteChanged(); IsNewChanged(); }; BankStatClient.UpdateOrInsertBankStatDetailCompleted += (s, x) => { var savedRow = SelectedMainRow.TblBankStatDetails.ElementAt(x.outindex); if (savedRow != null) { savedRow.InjectFrom(x.Result); savedRow.TblBankTransactionType1 = BankTransactionTypeList.FirstOrDefault(bt => bt.Iserial == savedRow.TblBankTransactionType); } RaisePropertyChanged(nameof(IsHeaderHasDetails)); }; BankStatClient.DeleteBankStatHeaderCompleted += (s, ev) => { if (ev.Error != null) { throw ev.Error; } var oldrow = MainRowList.FirstOrDefault(x => x.Iserial == ev.Result); if (oldrow != null) { MainRowList.Remove(oldrow); } }; BankStatClient.DeleteBankStatDetailCompleted += (s, ev) => { if (ev.Error != null) { throw ev.Error; } var oldrow = SelectedMainRow.TblBankStatDetails.FirstOrDefault(x => x.Iserial == ev.Result.Iserial); if (oldrow != null) { SelectedMainRow.TblBankStatDetails.Remove(oldrow); } RaisePropertyChanged(nameof(Total)); RaisePropertyChanged(nameof(IsHeaderHasDetails)); }; BankStatClient.IsBankStatHeaderHasMatchedRowsCompleted += (s, e) => { if (SelectedMainRow.Iserial == e.Iserial) { hasMatched = e.Result; } else { hasMatched = false; } RaisePropertyChanged(nameof(IsReadOnly)); }; BankStatClient.GetLookUpBankTransactionTypeCompleted += (s, e) => { BankTransactionTypeList = e.Result; }; BankStatClient.GetLookUpBankCompleted += (s, e) => { BankList = e.Result; }; BankStatClient.GetLookUpCurrencyCompleted += (s, e) => { CurrencyList = e.Result; }; BankStatClient.InsertImportedItemsCompleted += (s, e) => { for (int i = 0; i < RemainningImportList.Count; i = i + step) { bool approve = (i + step >= RemainningImportList.Count); //هل دى اخر لفة var temp = new ObservableCollection <ImportedBankStatement>(RemainningImportList.Skip(i).Take(step)); BankStatClient.InsertRemainingImportedItemsAsync(e.Result, temp, LoggedUserInfo.DatabasEname); // First Time requestes++; } ImportHeaderIserial = e.Result; Loading = false; }; BankStatClient.InsertRemainingImportedItemsCompleted += (s, e) => { requestes--;// على اساس ان الريكويست اسرع من الريسبونس foreach (var item in e.Result) { error += item + "\r\n"; } if (e.Error != null) { requestes = -1; throw e.Error; } else if (requestes == 0)// كده ده اخر واحد { if (string.IsNullOrWhiteSpace(error)) { MessageBox.Show("Import Completed Succesfully"); //هجيب الريكورد الى اتحفظ ده اعرضه بقى BankStatClient.GetBankStatHeaderByIserialAsync(ImportHeaderIserial, LoggedUserInfo.DatabasEname); ImportHeaderIserial = -1; return; } BankStatClient.DeleteBankStatByIserialAsync(ImportHeaderIserial, LoggedUserInfo.DatabasEname); ImportHeaderIserial = -1; if (MessageBox.Show("Import Completed, Do you want to view logs?", "Info", MessageBoxButton.OKCancel) == MessageBoxResult.OK) { new LogView(error).Show(); error = ""; } } }; BankStatClient.GetBankStatHeaderByIserialCompleted += (s, e) => { SelectedMainRow.InjectFrom(e.Result); SelectedMainRow.TblBank1 = BankList.FirstOrDefault(b => b.Iserial == SelectedMainRow.TblBank); SelectedMainRow.TblCurrency1 = CurrencyList.FirstOrDefault(c => c.Iserial == SelectedMainRow.TblCurrency); if (SelectedMainRow.TblBankStatDetails != null) { SelectedMainRow.TblBankStatDetails = new ObservableCollection <BankStatDetail>(); } RaisePropertyChanged(nameof(SelectedMainRow)); DeleteCommand.RaiseCanExecuteChanged(); ApproveBankStat.RaiseCanExecuteChanged(); DeleteBankStatDetail.RaiseCanExecuteChanged(); IsNewChanged(); GetDetailData(); }; BankStatClient.InsertExcelFileDateCompleted += (s, e) => { //if (e.Error != null) MessageBox.Show(e.Error.Message); }; ImportFromExcelCommand = new RelayCommand(() => { if (!ValidHeaderData()) { return; } OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Excel Files (*.xls)|*.xls"; if (ofd.ShowDialog() == true) { var importedList = new ObservableCollection <ImportedBankStatement>(); var fs = ofd.File.OpenRead(); var book = Workbook.Open(fs); var sheet = book.Worksheets[0]; var dataList = new ObservableCollection <Models.Excel.CellModel>(); for (int i = sheet.Cells.FirstColIndex; i < sheet.Cells.LastColIndex + 1; i++) { for (int j = sheet.Cells.FirstRowIndex; j < sheet.Cells.LastRowIndex + 1; j++) { var cellModel = new Models.Excel.CellModel() { Column = i, Row = j, Value = sheet.Cells[j, i].StringValue.ToUpper(), }; dataList.Add(cellModel); if (i == 1) { var t = sheet.Cells[j, i].StringValue.ToUpper().Split('-'); if (t.Length == 3) { cellModel.Day = t[0]; cellModel.Mounth = t[1]; cellModel.Year = t[2]; } } } } TblBankStatHeader headerRow = new TblBankStatHeader(); headerRow.InjectFrom(SelectedMainRow); BankStatClient.InsertExcelFileDateAsync(headerRow, dataList, LoggedUserInfo.DatabasEname);// new ObservableCollection<Models.Excel.CellModel>(dataList.Take(300))); //int docDateIndex = 0, transactionTypeIndex = 0, descriptionIndex = 0, amountIndex = 0, chequeNoIndex = 0, depositNoIndex = 0; //FillList(importedList, sheet, ref docDateIndex, ref transactionTypeIndex, ref descriptionIndex, ref amountIndex, ref chequeNoIndex, ref depositNoIndex); //InsertImportedDetail(importedList); } }); MatchCommand = new RelayCommand(() => { if (SelectedMainRow.Iserial <= 0) { MessageBox.Show("Bank Statement must save first"); return; } var matchingView = new BankStatementMatchView(); var matchingViewModel = new BankStatementMatchViewModel(); matchingViewModel.HeaderRow = SelectedMainRow; matchingView.DataContext = matchingViewModel; matchingView.Show(); }); GetComboData(); GetMaindata(); } }