public TblBrandStoreTargetHeaderViewModel()
        {
            MainRowList = new SortableCollectionView <TblBrandStoreTargetDetailViewModel>();

            MainRowList.CollectionChanged += MainRowList_CollectionChanged;
            _client.GetTblBrandStoreTargetHeaderCompleted += (s, sv) =>
            {
                Glserial = 0;
                MainRowList.Clear();
                if (sv.Result != null)
                {
                    foreach (var tblBrandStoreTargetDetail in sv.Result.TblBrandStoreTargetDetails.OrderBy(x => x.TblStore1.code))
                    {
                        Glserial = tblBrandStoreTargetDetail.Glserial;
                        var row =
                            new TblBrandStoreTargetDetailViewModel().InjectFrom(tblBrandStoreTargetDetail) as
                            TblBrandStoreTargetDetailViewModel;
                        row.StorePerRow = new StoresForBrand
                        {
                            Iserial = tblBrandStoreTargetDetail.TblStore1.iserial,
                            Code    = tblBrandStoreTargetDetail.TblStore1.code,
                            Ename   = tblBrandStoreTargetDetail.TblStore1.ENAME,
                            Aname   = tblBrandStoreTargetDetail.TblStore1.aname
                        };

                        MainRowList.Add(row);
                    }
                    Glserial = sv.Result.Glserial;
                }
                foreach (var variable in sv.Stores.OrderBy(x => x.Code))
                {
                    if (MainRowList.FirstOrDefault(x => x.Tblstore == variable.Iserial) == null)
                    {
                        MainRowList.Add(new TblBrandStoreTargetDetailViewModel
                        {
                            Glserial    = Glserial,
                            StorePerRow = variable,
                            Qty         = 1
                        });
                    }
                }

                TotalAmount = MainRowList.Sum(x => x.Amount);

                TotalQty = MainRowList.Sum(x => x.Qty);
            };
        }
        public BrandStoreTargetViewModel()
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                YearList = new List <int>();
                for (var i = 0; i < 10; i++)
                {
                    const int yearNumber = 2014;
                    YearList.Add(yearNumber + i);
                }
                TransactionHeader.Year = DateTime.Now.Year;
                GetItemPermissions(PermissionItemName.BrandStoreTarget.ToString());
                Client.ServerConnectionsAsync(LoggedUserInfo.Ip, LoggedUserInfo.Port, LoggedUserInfo.DatabasEname);
                SelectedMainRow = new TblBrandStoreTargetDetailViewModel();

                Client.UpdateOrInsertTblBrandStoreTargetDetailCompleted += (s, x) =>
                {
                    var savedRow = (TblBrandStoreTargetDetailViewModel)TransactionHeader.MainRowList.GetItemAt(x.outindex);

                    TransactionHeader.Glserial = x.Result.Glserial;
                    if (savedRow != null)
                    {
                        savedRow.InjectFrom(x.Result);
                    }
                };
                Client.DeleteTblBrandStoreTargetDetailCompleted += (s, ev) =>
                {
                    if (ev.Error != null)
                    {
                        throw ev.Error;
                    }

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