public void AddNewDetailRow(bool checkLastRow)
        {
            var currentRowIndex = (SelectedMainRow.DetailsList.IndexOf(SelectedDetailRow));

            if (SelectedDetailRow == null)
            {
                currentRowIndex = 0;
            }
            if (checkLastRow)
            {
                var lastrow             = SelectedMainRow.DetailsList.ElementAtOrDefault(currentRowIndex);
                var valiationCollection = new List <ValidationResult>();

                var isvalid = Validator.TryValidateObject(lastrow,
                                                          new ValidationContext(lastrow, null, null), valiationCollection, true);

                if (!isvalid)
                {
                    return;
                }
            }
            if (AllowUpdate != true)
            {
                return;
            }


            var newrow = new TblJournalSettingEntityViewModel
            {
                TblJournalSetting = SelectedMainRow.Iserial,
            };

            if (SelectedMainRow.DetailsList.Any(x => x.Iserial > -1))
            {
                SelectedMainRow.DetailsList.Insert(currentRowIndex + 1, newrow);
            }
            else
            {
                if (currentRowIndex == -1)
                {
                    currentRowIndex = 0;
                }
                SelectedMainRow.DetailsList.Insert(currentRowIndex, newrow);
            }

            SelectedDetailRow = newrow;
            if (ExportGrid != null)
            {
                ExportGrid.BeginEdit();
            }
        }
        public void SaveOldDetailRow(TblJournalSettingEntityViewModel oldRow)
        {
            if (oldRow != null)
            {
                var valiationCollection = new List <ValidationResult>();

                var isvalid = Validator.TryValidateObject(oldRow,
                                                          new ValidationContext(oldRow, null, null), valiationCollection, true);

                if (isvalid)
                {
                    var save = oldRow.Iserial == 0;
                    if (oldRow.TblJournalSetting == 0)
                    {
                        oldRow.TblJournalSetting = SelectedMainRow.Iserial;
                    }
                    if (AllowUpdate != true)
                    {
                        return;
                    }
                    var rowToSave = new TblJournalSettingEntity();
                    rowToSave.InjectFrom(oldRow);

                    if (!Loading)
                    {
                        if (SelectedMainRow != null)
                        {
                            if (SelectedMainRow.DetailsList.IndexOf(oldRow) != -1)
                            {
                                Loading = true;
                                Glclient.UpdateOrInsertTblJournalSettingEntitysAsync(rowToSave, save,
                                                                                     SelectedMainRow.DetailsList.IndexOf(oldRow), LoggedUserInfo.DatabasEname,
                                                                                     LoggedUserInfo.Iserial);
                            }
                        }
                    }
                }
            }
        }
        public JournalSettingViewModel()
        {
            if (!IsDesignTime)
            {
                GetItemPermissions(PermissionItemName.JournalSetting.ToString());
                Glclient = new GlServiceClient();

                MainRowList     = new ObservableCollection <TblJournalSettingViewModel>();
                SelectedMainRow = new TblJournalSettingViewModel();

                var journalAccountTypeClient = new GlServiceClient();
                journalAccountTypeClient.GetGenericCompleted += (s, sv) =>
                {
                    JournalAccountTypeList = sv.Result;
                };
                journalAccountTypeClient.GetGenericAsync("TblJournalAccountType", "%%", "%%", "%%", "Iserial", "ASC", LoggedUserInfo.DatabasEname);


                Glclient.GetTblJournalSettingCompleted += (s, sv) =>
                {
                    foreach (var row in sv.Result)
                    {
                        var newrow = new TblJournalSettingViewModel();
                        newrow.InjectFrom(row);

                        MainRowList.Add(newrow);
                    }

                    Loading   = false;
                    FullCount = sv.fullCount;
                    if (MainRowList.Any() && (SelectedMainRow == null))
                    {
                        SelectedMainRow = MainRowList.FirstOrDefault();
                    }
                    if (FullCount == 0 && MainRowList.Count == 0)
                    {
                        AddNewMainRow(false);
                    }
                    if (Export)
                    {
                        Export      = false;
                        AllowExport = true;
                        //ExportGrid.ExportExcel("Account");
                    }
                };

                Glclient.UpdateOrInsertTblJournalSettingsCompleted += (s, ev) =>
                {
                    if (ev.Error != null)
                    {
                        MessageBox.Show(ev.Error.Message);
                    }
                    try
                    {
                        MainRowList.ElementAt(ev.outindex).InjectFrom(ev.Result);
                    }
                    catch (Exception)
                    {
                    }
                    Loading = false;
                };
                Glclient.DeleteTblJournalSettingCompleted += (s, ev) =>
                {
                    if (ev.Error != null)
                    {
                        MessageBox.Show(ev.Error.Message);
                    }
                    foreach (var item in ev.Result.ToList())
                    {
                        var oldrow = MainRowList.FirstOrDefault(x => x.Iserial == item);
                        if (oldrow != null)
                        {
                            MainRowList.Remove(oldrow);
                        }
                    }

                    if (!MainRowList.Any())
                    {
                        AddNewMainRow(false);
                    }
                };

                Glclient.GetTblJournalSettingEntityCompleted += (s, sv) =>
                {
                    foreach (var row in sv.Result)
                    {
                        var newrow = new TblJournalSettingEntityViewModel();
                        newrow.JournalAccountTypePerRow = new GenericTable();
                        newrow.JournalAccountTypePerRow.InjectFrom(row.TblJournalAccountType1);

                        newrow.JournalPerRow = new TblJournal();
                        newrow.JournalPerRow.InjectFrom(row.TblJournal1);
                        newrow.EntityPerRow =
                            sv.entityList.FirstOrDefault(x => x.TblJournalAccountType == row.TblJournalAccountType &&
                                                         x.Iserial == row.EntityAccount && x.scope == row.Scope);
                        newrow.InjectFrom(row);
                        SelectedMainRow.DetailsList.Add(newrow);
                    }
                    Loading         = false;
                    DetailFullCount = sv.fullCount;
                    if (SelectedMainRow.DetailsList.Any() && (SelectedDetailRow == null))
                    {
                        SelectedDetailRow = SelectedMainRow.DetailsList.FirstOrDefault();
                    }

                    if (DetailFullCount == 0 && SelectedMainRow.DetailsList.Count(x => x.Iserial >= 0) == 0)
                    {
                        AddNewDetailRow(false);
                    }

                    if (Export)
                    {
                        AllowExport = true;
                        Export      = false;
                    }
                };

                Glclient.UpdateOrInsertTblJournalSettingEntitysCompleted += (s, x) =>
                {
                    var savedRow = SelectedMainRow.DetailsList.ElementAt(x.outindex);
                    if (savedRow != null)
                    {
                        savedRow.InjectFrom(x.Result);
                    }
                    Loading = false;
                };

                Glclient.DeleteTblJournalSettingEntityCompleted += (s, ev) =>
                {
                    Loading = false;

                    if (ev.Error != null)
                    {
                        MessageBox.Show(ev.Error.Message);
                    }
                    foreach (var item in ev.Result.ToList())
                    {
                        var oldrow = SelectedMainRow.DetailsList.FirstOrDefault(x => x.Iserial == item);
                        if (oldrow != null)
                        {
                            SelectedMainRow.DetailsList.Remove(oldrow);
                        }
                    }
                    if (!SelectedMainRow.DetailsList.Any())
                    {
                        AddNewDetailRow(false);
                    }
                };

                var glRuleTypeClient = new GlServiceClient();
                glRuleTypeClient.GetGenericCompleted += (s, sv) => { CostCenterTypeList = sv.Result; };
                glRuleTypeClient.GetGenericAsync("TblCostCenterType", "%%", "%%", "%%", "Iserial", "ASC",
                                                 LoggedUserInfo.DatabasEname);

                Glclient.GetTblJournalSettingCostCenterCompleted += (s, sv) =>
                {
                    foreach (var row in sv.Result)
                    {
                        var newrow = new TblJournalSettingCostCenterViewModel();
                        if (row.TblCostCenter1 != null)
                        {
                            newrow.CostCenterPerRow = new TblCostCenter();
                            newrow.CostCenterPerRow.InjectFrom(row.TblCostCenter1);

                            if (row.TblCostCenter1.TblCostCenterType1 != null)
                            {
                                newrow.CostCenterTypePerRow.InjectFrom(row.TblCostCenter1.TblCostCenterType1);
                            }
                        }


                        newrow.InjectFrom(row);

                        SelectedMainRow.SubDetailList.Add(newrow);
                    }
                    Loading            = false;
                    DetailSubFullCount = sv.fullCount;

                    if (SelectedMainRow.SubDetailList.Any() &&
                        (SelectedSubDetailRow == null))
                    {
                        SelectedSubDetailRow = SelectedMainRow.SubDetailList.FirstOrDefault();
                    }

                    if (DetailSubFullCount == 0 && SelectedMainRow.SubDetailList.Count(x => x.Iserial >= 0) == 0)
                    {
                        AddNewSubDetailRow(false);
                    }
                    if (ExportGrid != null)
                    {
                        ExportGrid.ScrollIntoView(SelectedSubDetailRow, ExportGrid.Columns[1]);
                        ExportGrid.CurrentColumn = ExportGrid.Columns[1];
                        ExportGrid.Focus();
                    }
                };

                Glclient.UpdateOrInsertTblJournalSettingCostCentersCompleted += (s, x) =>
                {
                    var savedRow = SelectedMainRow.SubDetailList.ElementAt(x.outindex);
                    if (savedRow != null)
                    {
                        savedRow.InjectFrom(x.Result);
                    }
                    Loading = false;
                };

                Glclient.DeleteTblJournalSettingCostCenterCompleted += (s, ev) =>
                {
                    if (ev.Error != null)
                    {
                        MessageBox.Show(ev.Error.Message);
                    }
                    foreach (var item in ev.Result.ToList())
                    {
                        var oldrow = SelectedMainRow.DetailsList.FirstOrDefault(x => x.Iserial == item);
                        if (oldrow != null)
                        {
                            SelectedMainRow.DetailsList.Remove(oldrow);
                        }
                    }
                    if (!SelectedMainRow.SubDetailList.Any())
                    {
                        AddNewSubDetailRow(false);
                    }
                };

                GetMaindata();
            }
        }