public void AddNewMainRow(bool checkLastRow)
        {
            var currentRowIndex = (MainRowList.IndexOf(SelectedMainRow));

            if (checkLastRow)
            {
                var valiationCollection = new List <ValidationResult>();

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

                if (!isvalid)
                {
                    return;
                }
            }
            if (AllowAdd != true)
            {
                MessageBox.Show(strings.AllowAddMsg);
                return;
            }
            var newrow = new TblInventPostingViewModel
            {
                TblInventAccountType     = TblInventAccountType,
                JournalAccountTypePerRow = JournalAccountTypePerRow
            };

            MainRowList.Insert(currentRowIndex + 1, newrow);
            SelectedMainRow = newrow;
        }
        public InventPostingViewModel()
        {
            if (!IsDesignTime)
            {
                GetItemPermissions(PermissionItemName.InventPosting.ToString());
                Glclient        = new GlServiceClient();
                MainRowList     = new SortableCollectionView <TblInventPostingViewModel>();
                SelectedMainRow = new TblInventPostingViewModel();
                Glclient.GetGlItemGroupAsync(LoggedUserInfo.DatabasEname);
                Glclient.GetGlItemGroupCompleted += (s, sv) =>
                {
                    ItemGroupList = sv.Result;
                };
                Glclient.GetTblInventPostingTypeAsync(LoggedUserInfo.DatabasEname);
                Glclient.GetTblInventPostingTypeCompleted += (s, sv) =>
                {
                    InventPostingTypeList = sv.Result;
                    Items = new ObservableCollection <TblInventAccountType>(InventPostingTypeList.Where(x => x.TabName == "SalesOrder"));
                };
                Glclient.GetTblInventPostingCompleted += (s, sv) =>
                {
                    foreach (var row in sv.Result)
                    {
                        var newrow = new TblInventPostingViewModel
                        {
                            AccountPerRow   = row.TblAccount1,
                            ItemGroupPerRow = ItemGroupList.FirstOrDefault(x => x.Iserial == row.ItemScopeRelation)
                        };
                        newrow.InjectFrom(row);
                        newrow.JournalAccountTypePerRow = JournalAccountTypePerRow;
                        if (sv.entityList != null && sv.entityList.Any(x => x.scope == row.SupCustScope && x.Iserial == row.SupCustRelation))
                        {
                            newrow.EntityAccountPerRow =
                                sv.entityList.First(x => x.scope == row.SupCustScope && x.Iserial == row.SupCustRelation);
                        }

                        MainRowList.Add(newrow);
                    }

                    Loading = false;

                    if (MainRowList.Any() && (SelectedMainRow == null))
                    {
                        SelectedMainRow = MainRowList.FirstOrDefault();
                    }
                    if (MainRowList.Count == 0)
                    {
                        AddNewMainRow(false);
                    }
                };

                Glclient.UpdateOrInsertTblInventPostingsCompleted += (s, ev) =>
                {
                    if (ev.Error != null)
                    {
                        MessageBox.Show(ev.Error.Message);
                    }
                    try
                    {
                        MainRowList.ElementAt(ev.outindex).InjectFrom(ev.Result);
                    }
// ReSharper disable once EmptyGeneralCatchClause
                    catch
                    {
                    }
                };
                Glclient.DeleteTblInventPostingCompleted += (s, ev) =>
                {
                    if (ev.Error != null)
                    {
                        MessageBox.Show(ev.Error.Message);
                    }

                    var oldrow = MainRowList.FirstOrDefault(x => x.Iserial == ev.Result);
                    if (oldrow != null)
                    {
                        MainRowList.Remove(oldrow);
                    }
                    if (!MainRowList.Any())
                    {
                        AddNewMainRow(false);
                    }
                };
            }
        }