Exemple #1
0
        private void ValidateDetailRow(BankStatDetail bankStatDetail)
        {
            var valiationCollection = new List <ValidationResult>();
            var isvalid             = Validator.TryValidateObject(SelectedDetailRow, new ValidationContext(SelectedDetailRow, null, null), valiationCollection, true);

            if (!isvalid)
            {
                SelectedMainRow.TblBankStatDetails.Remove(SelectedDetailRow);
                SelectedMainRow.TblBankStatDetails.Add(bankStatDetail);
            }
            else
            {
                SelectedMainRow.TblBankStatDetails.Add(bankStatDetail);
            }
            SelectedDetailRow = bankStatDetail;
            RaisePropertyChanged(nameof(Total));
            RaisePropertyChanged(nameof(IsHeaderHasDetails));
        }
Exemple #2
0
        public void AddNewDetailRow(bool checkLastRow)
        {
            var currentRowIndex = (SelectedMainRow.TblBankStatDetails.IndexOf(SelectedDetailRow));

            if (!checkLastRow || currentRowIndex == (SelectedMainRow.TblBankStatDetails.Count - 1))
            {
                if (checkLastRow && SelectedDetailRow != null)
                {
                    var valiationCollection = new List <ValidationResult>();
                    var isvalid             = Validator.TryValidateObject(SelectedDetailRow, new ValidationContext(
                                                                              SelectedDetailRow, null, null), valiationCollection, true);
                    if (!isvalid)
                    {
                        return;
                    }
                }
                SelectedMainRow.TblBankStatDetails.Insert(currentRowIndex + 1, SelectedDetailRow = new BankStatDetail
                {
                    TblBankStatHeader = SelectedMainRow.Iserial
                });
                RaisePropertyChanged(nameof(Total));
                RaisePropertyChanged(nameof(IsHeaderHasDetails));
            }
        }
        public BankStatementMatchViewModel() : base(PermissionItemName.BankStatement)
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                LoadingDetailRows = new RelayCommand <object>((o) =>
                {
                    var e = o as DataGridRowEventArgs;
                    if (HeaderRow.TblBankStatDetails.Count < PageSize)
                    {
                        return;
                    }
                    if (HeaderRow.TblBankStatDetails.Count - 2 < e.Row.GetIndex() && !Loading)
                    {
                        GetMatchedData();
                    }
                });
                BankStatClient.InsertMatchedListCompleted += (s, e) =>
                {
                    if (!e.lastInsert)
                    {
                        requests--;
                        if (requests == 0)
                        {
                            GetUnMatcheddata();
                            GetMatchedData();
                        }
                    }
                    else
                    {
                        GetUnMatcheddata();
                        GetMatchedData();
                    }
                };
                BankStatClient.RemoveMatchedListCompleted += (s, e) =>
                {
                    GetUnMatcheddata();
                    GetMatchedData();
                };
                SelectionChanged = new RelayCommand <object>((o) => { MatchSelected.RaiseCanExecuteChanged(); });

                BankStatClient.GetBankStatDetailForMatchingCompleted += (s, e) => {
                    BankStatDetailList = e.Result;
                    ApproveAsMatched.RaiseCanExecuteChanged();
                };
                BankStatClient.GetLedgerDetailForMatchingCompleted += (s, e) => {
                    LedgerDetailList = e.Result;
                    ApproveAsMatched.RaiseCanExecuteChanged();
                };
                BankStatClient.GetBankStatDetailMatchedCompleted += (s, e) => {
                    BankStatDetailMatchedList = e.Result;
                    ApproveAsMatched.RaiseCanExecuteChanged();
                };
                BankStatClient.GetLedgerDetailMatchedCompleted += (s, e) => {
                    LedgerDetailMatchedList = e.Result;
                    ApproveAsMatched.RaiseCanExecuteChanged();
                };

                BankStatClient.GetLedgerDetailMatchedByBankStatDetailIdCompleted += (s, e) =>
                {
                    foreach (var item in e.Result)
                    {
                        LedgerDetailMatchedList.FirstOrDefault(ldm => ldm.Iserial == item.Iserial).IsChecked = item.IsChecked;
                    }
                };
                BankStatClient.GetBankStatDetailMatchedByLedgerDetailIdCompleted += (s, e) =>
                {
                    foreach (var item in e.Result)
                    {
                        BankStatDetailMatchedList.FirstOrDefault(ldm => ldm.Iserial == item.Iserial).IsChecked = item.IsChecked;
                    }
                };

                BankStatClient.UpdateOrInsertBankStatHeaderCompleted += (s, x) =>
                {
                    BankStatHeader savedRow = new BankStatHeader();

                    if (savedRow != null)
                    {
                        savedRow.InjectFrom(x.Result);
                        savedRow.TblBankStatDetails.Clear();
                        foreach (var item in x.Result.TblBankStatDetails)
                        {
                            var detailTemp = new BankStatDetail();
                            detailTemp.InjectFrom(item);
                            savedRow.TblBankStatDetails.Add(detailTemp);
                        }
                    }
                    HeaderRow = savedRow;
                    IsNewChanged();
                };

                BankStatChecked = new RelayCommand <object>((o) => {
                    var row = ((CheckBox)((RoutedEventArgs)o).OriginalSource).DataContext as BankStatMatchingModel;
                    // كده هو اختارها
                    BankStatClient.GetLedgerDetailMatchedByBankStatDetailIdAsync(
                        HeaderRow.Iserial, row.Iserial, true, LoggedUserInfo.DatabasEname);
                });
                BankStatUnchecked = new RelayCommand <object>((o) => {
                    var row = ((CheckBox)((RoutedEventArgs)o).OriginalSource).DataContext as BankStatMatchingModel;
                    // كده هو لغاها
                    BankStatClient.GetLedgerDetailMatchedByBankStatDetailIdAsync(
                        HeaderRow.Iserial, row.Iserial, false, LoggedUserInfo.DatabasEname);
                });
                LedgerMainDetailChecked = new RelayCommand <object>((o) => {
                    var row = ((CheckBox)((RoutedEventArgs)o).OriginalSource).DataContext as BankStatMatchingModel;
                    // كده هو اختارها
                    BankStatClient.GetBankStatDetailMatchedByLedgerDetailIdAsync(
                        HeaderRow.Iserial, row.Iserial, true, LoggedUserInfo.DatabasEname);
                });
                LedgerMainDetailUnchecked = new RelayCommand <object>((o) => {
                    var row = ((CheckBox)((RoutedEventArgs)o).OriginalSource).DataContext as BankStatMatchingModel;
                    // كده هو لغاها
                    BankStatClient.GetBankStatDetailMatchedByLedgerDetailIdAsync(
                        HeaderRow.Iserial, row.Iserial, false, LoggedUserInfo.DatabasEname);
                });

                MatchSelected = new RelayCommand <object>((o) => {
                    var selectedBankStatDetailList = new ObservableCollection <BankStatMatchingModel>();
                    BankStatDetailList.ForEach(bs => { if (bs.IsChecked)
                                                       {
                                                           selectedBankStatDetailList.Add(bs);
                                                       }
                                               });
                    var selectedLedgerDetailList = new ObservableCollection <BankStatMatchingModel>();
                    LedgerDetailList.ForEach(bs => { if (bs.IsChecked)
                                                     {
                                                         selectedLedgerDetailList.Add(bs);
                                                     }
                                             });
                    if (selectedBankStatDetailList.Count() <= 0 || selectedLedgerDetailList.Count() <= 0)
                    {
                        MessageBox.Show("You must select from both parties");
                        return;
                    }
                    if (!(selectedBankStatDetailList.Count() == 1 || selectedLedgerDetailList.Count() == 1))
                    {
                        MessageBox.Show("You must select one record from one of the parties");
                        return;
                    }
                    if (Math.Abs(selectedBankStatDetailList.Sum(s => s.Amount) - selectedLedgerDetailList.Sum(s => s.Amount)) > acceptedAmountDifferance)
                    {
                        MessageBox.Show("Amounts must be equals");
                        return;
                    }
                    BankStatClient.InsertMatchedListAsync(selectedBankStatDetailList,
                                                          selectedLedgerDetailList, true, LoggedUserInfo.DatabasEname);
                    //هتعمل ريكويس ياخد الليستيتين ويضيف ريكوردات فى الداتابيز ويرجع هنا لما يرجع تجيب الريكوردات دى وتضيفها تحت وهتدور بالسريال الى معاك
                    //والحذف نفس الكلام هروح احذفهم وارجع احذف من عندى
                    //وفى الحالتين الى بينضاف تحت يتحذف من فوق والى يتحذف من تحت ينضاف فوق او تعمل ريفريش لكله
                });
                UnMatchSelected = new RelayCommand <object>((o) => {
                    var selectedBankStatDetailList = new ObservableCollection <BankStatMatchingModel>();
                    BankStatDetailMatchedList.ForEach(bs => { if (bs.IsChecked)
                                                              {
                                                                  selectedBankStatDetailList.Add(bs);
                                                              }
                                                      });
                    var selectedLedgerDetailList = new ObservableCollection <BankStatMatchingModel>();
                    LedgerDetailMatchedList.ForEach(bs => { if (bs.IsChecked)
                                                            {
                                                                selectedLedgerDetailList.Add(bs);
                                                            }
                                                    });
                    if (selectedBankStatDetailList.Count() <= 0 || selectedLedgerDetailList.Count() <= 0)
                    {
                        MessageBox.Show("You must select from both parties");
                        return;
                    }
                    if (!(selectedBankStatDetailList.Count() == 1 || selectedLedgerDetailList.Count() == 1))
                    {
                        MessageBox.Show("You must select one record from one of the parties");
                        return;
                    }
                    if (selectedBankStatDetailList.Sum(s => s.Amount) != selectedLedgerDetailList.Sum(s => s.Amount))
                    {
                        MessageBox.Show("Amounts must be equals");
                        return;
                    }
                    BankStatClient.RemoveMatchedListAsync(selectedBankStatDetailList,
                                                          selectedLedgerDetailList, true, LoggedUserInfo.DatabasEname);
                });

                AutoMatch = new RelayCommand <object>((o) => {
                    var w      = new BankStatementAutoMatchTypeView();
                    w.Closing += (s, e) => {
                        if (w.DialogResult ?? false)//كده هو اختار حاجة
                        {
                            requests            = 0;
                            bool transType      = w.TransactionType.IsChecked ?? false,
                            chequeNo            = w.ChequeNo.IsChecked ?? false,
                            depositNo           = w.DepositNo.IsChecked ?? false,
                            amount              = w.Amount.IsChecked ?? false;
                            int count           = 0;
                            var bankStatMinDate = BankStatDetailList.Min(r => r.DocDate);
                            // كده معاك الى اختاره
                            BankStatDetailList.ForEach(bsd => bsd.IsChecked = false);
                            LedgerDetailList.ForEach(ld => ld.IsChecked     = false);
                            if (transType)
                            {
                            }                 /////احنا اصلا مش بنسجله
                            if (chequeNo)
                            {
                                BankStatDetailList.Where(bsd =>
                                                         bsd.ChequeNo != null /*&& !bsd.IsChecked*/).ForEach(bsd => {
                                    var chequeRows = new ObservableCollection <BankStatMatchingModel>();
                                    LedgerDetailList.Where(ld => ld.ChequeNo != null &&
                                                           ld.ChequeNo == bsd.ChequeNo && !ld.IsChecked)
                                    .ForEach(ld => { chequeRows.Add(ld); });
                                    if (chequeRows != null && Math.Abs(chequeRows.Sum(c => c.Amount) - bsd.Amount) <= acceptedAmountDifferance)
                                    {
                                        bsd.IsChecked = true; chequeRows.ForEach(a => a.IsChecked = true);
                                        BankStatClient.InsertMatchedListAsync(
                                            new ObservableCollection <BankStatMatchingModel>()
                                        {
                                            bsd
                                        },
                                            chequeRows, false, LoggedUserInfo.DatabasEname);
                                        requests++; count++;
                                        return;
                                    }
                                });
                            }
                            if (depositNo)
                            {
                                //BankStatDetailList.Where(bsd =>
                                //   !string.IsNullOrEmpty(bsd.DepositNo)/* && !bsd.IsChecked*/).ForEach(bsd => {
                                //       var depositRows = new ObservableCollection<BankStatMatchingModel>();
                                //       LedgerDetailList.Where(ld => !string.IsNullOrEmpty(ld.DepositNo) &&
                                //       ld.DepositNo == bsd.DepositNo && !ld.IsChecked)
                                //       .ForEach(ld => { depositRows.Add(ld); });
                                //       if (depositRows != null && Math.Abs(depositRows.Sum(c => c.Amount) - bsd.Amount) <= acceptedAmountDifferance)
                                //       {
                                //           bsd.IsChecked = true; depositRows.ForEach(a => a.IsChecked = true);
                                //           BankStatClient.InsertMatchedListAsync(
                                //               new ObservableCollection<BankStatMatchingModel>() { bsd },
                                //               depositRows, false);
                                //           requests++; count++;
                                //           return;
                                //       }
                                //   });

                                BankStatDetailList.Where(bsd =>
                                                         !string.IsNullOrEmpty(bsd.DepositNo) /* && !bsd.IsChecked*/).GroupBy(bsd =>
                                                                                                                              new { bsd.DepositNo, bsd.IsChecked }).ForEach(bsd => {
                                    var depositRows = new ObservableCollection <BankStatMatchingModel>();
                                    LedgerDetailList.Where(ld => !string.IsNullOrEmpty(ld.DepositNo) &&
                                                           ld.DepositNo == bsd.Key.DepositNo && !ld.IsChecked)
                                    .ForEach(ld => { depositRows.Add(ld); });
                                    if (depositRows != null && Math.Abs(depositRows.Sum(c => c.Amount) - bsd.Sum(r => r.Amount)) <= acceptedAmountDifferance)
                                    {
                                        bsd.ForEach(r => r.IsChecked = true); depositRows.ForEach(a => a.IsChecked = true);
                                        BankStatClient.InsertMatchedListAsync(
                                            bsd.ToObservableCollection(),
                                            depositRows, false, LoggedUserInfo.DatabasEname);
                                        requests++; count++;
                                        return;
                                    }
                                    else if (depositRows.Count > 1)
                                    {
                                        depositRows = new ObservableCollection <BankStatMatchingModel>();
                                        LedgerDetailList.Where(ld => !string.IsNullOrEmpty(ld.DepositNo) &&
                                                               ld.DocDate > bankStatMinDate && !ld.IsChecked)
                                        .Where(ld => ld.DepositNo == bsd.Key.DepositNo)
                                        .ForEach(ld => { depositRows.Add(ld); });
                                        if (depositRows != null && Math.Abs(depositRows.Sum(c => c.Amount) - bsd.Sum(r => r.Amount)) <= acceptedAmountDifferance)
                                        {
                                            bsd.ForEach(r => r.IsChecked = true); depositRows.ForEach(a => a.IsChecked = true);
                                            BankStatClient.InsertMatchedListAsync(
                                                bsd.ToObservableCollection(),
                                                depositRows, false, LoggedUserInfo.DatabasEname);
                                            requests++; count++;
                                            return;
                                        }
                                    }
                                });
                            }
                            if (amount)
                            {
                                //هتعمل ايه لو اكتر من سجل له نفس المبلغ
                                BankStatDetailList /*.Where(bsd => !bsd.IsChecked)*/.ForEach(bsd =>
                                {
                                    //var amountRows = new ObservableCollection<BankStatMatchingModel>();
                                    var firstMatch = LedgerDetailList.FirstOrDefault(ld => (Math.Abs(ld.Amount - bsd.Amount) <= acceptedAmountDifferance) &&
                                                                                     ld.DocDate == bsd.DocDate &&
                                                                                     !ld.IsChecked);
                                    if (firstMatch != null)
                                    {
                                        bsd.IsChecked = true; firstMatch.IsChecked = true;
                                        BankStatClient.InsertMatchedListAsync(
                                            new ObservableCollection <BankStatMatchingModel>()
                                        {
                                            bsd
                                        },
                                            new ObservableCollection <BankStatMatchingModel>()
                                        {
                                            firstMatch
                                        }, false, LoggedUserInfo.DatabasEname);
                                        requests++; count++;
                                    }
                                    //.ForEach(ld => { amountRows.Add(ld); });
                                    //if (amountRows != null && amountRows.Sum(c => c.Amount) == bsd.Amount &&
                                    //    !amountRows.Any(c => c.DocDate.Value.Date != bsd.DocDate.Value.Date))
                                    ////طيب لو الحركة عبارة عن جزء من الحركات يعنى فيه كذا حركة بالف انا هماتش مع واحدة مش كله
                                    //{
                                    //    BankStatClient.InsertMatchedListAsync(
                                    //        new ObservableCollection<BankStatMatchingModel>() { bsd },
                                    //        amountRows, false);
                                    //    requests++; count++;
                                    //    bsd.IsChecked = true; amountRows.ForEach(a => a.IsChecked = true);
                                    //    return;
                                    //}
                                });
                            }
                            // يا تعرف ان ده اخر واحد يا تبعت واحد فاضى فى الاخر بعدهم
                            if (count == 0)
                            {
                                MessageBox.Show("No Match Found");
                            }
                        }
                    };
                    w.Show();
                });
                UnMatchAll = new RelayCommand <object>((o) => {
                    BankStatDetailMatchedList.ForEach(bs => { if (!bs.IsChecked)
                                                              {
                                                                  bs.IsChecked = true;
                                                              }
                                                      });
                    LedgerDetailMatchedList.ForEach(bs => { if (!bs.IsChecked)
                                                            {
                                                                bs.IsChecked = true;
                                                            }
                                                    });
                    if (Math.Abs(BankStatDetailMatchedList.Sum(s => s.Amount) - LedgerDetailMatchedList.Sum(s => s.Amount)) > acceptedAmountDifferance)
                    {
                        MessageBox.Show("Amounts must be equals");
                        return;
                    }
                    BankStatClient.RemoveMatchedListAsync(BankStatDetailMatchedList,
                                                          LedgerDetailMatchedList, true, LoggedUserInfo.DatabasEname);
                });

                ApproveAsMatched = new RelayCommand <object>((o) => {
                    HeaderRow.MatchApproved    = true;
                    HeaderRow.MatchApproveDate = DateTime.Now;
                    HeaderRow.MatchApprovedBy  = LoggedUserInfo.Iserial;
                    BankStatClient.UpdateOrInsertBankStatHeaderAsync((TblBankStatHeader)
                                                                     new TblBankStatHeader().InjectFrom(HeaderRow), 0,
                                                                     LoggedUserInfo.Iserial, LoggedUserInfo.DatabasEname);
                },
                                                             (o) => {
                    return(BankStatDetailList.Count == 0 &&
                           BankStatDetailMatchedList.Count > 0);
                });
            }
        }
Exemple #4
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();
            }
        }