public void AddNewMainRow(bool checkLastRow)
        {
            if (AllowAdd != true)
            {
                MessageBox.Show(strings.AllowAddMsg);
                return;
            }
            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;
                }
            }

            var sizeRatioList = new ObservableCollection <TblSeasonalMasterListDetail>();

            foreach (var sizeCode in SelectedStyleViewModel.Sizes.OrderBy(x => x.Id))
            {
                sizeRatioList.Add(new TblSeasonalMasterListDetail
                {
                    Size = sizeCode.SizeCode,
                });
            }

            var style = new TblStyle();

            style.InjectFrom(SelectedStyleViewModel.SelectedMainRow);

            var prevRow = new TblSeasonalMasterListViewModel();

            if (currentRowIndex != -1)
            {
                prevRow = MainRowList.ElementAt(currentRowIndex);
            }

            var newrow = new TblSeasonalMasterListViewModel
            {
                TblStyle    = SelectedStyleViewModel.SelectedMainRow.Iserial,
                StylePerRow = style,
            };

            GenericMapper.InjectFromObCollection(newrow.SeasonalMasterListDetail,
                                                 prevRow.SeasonalMasterListDetail.Count() != 0 ? prevRow.SeasonalMasterListDetail : sizeRatioList);

            newrow.ThemePerRow = prevRow.ThemePerRow;

            foreach (var size in newrow.SeasonalMasterListDetail)
            {
                size.ProductionPerSize = 0;
            }

            MainRowList.Insert(currentRowIndex + 1, newrow);
            SelectedMainRow = newrow;
        }
        public SeasonalMasterListViewModel(StyleHeaderViewModel selectedStyle)
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                GetItemPermissions(PermissionItemName.SeasonalMasterListForm.ToString());
                SelectedStyleViewModel         = selectedStyle;
                MainRowList                    = new ObservableCollection <TblSeasonalMasterListViewModel>();
                SelectedMainRow                = new TblSeasonalMasterListViewModel();
                MainRowList.CollectionChanged += MainRowList_CollectionChanged;

                if (LoggedUserInfo.AllowedStores != null)
                {
                    Client.SearchBysStoreNameAsync(new ObservableCollection <int>(LoggedUserInfo.AllowedStores), LoggedUserInfo.Iserial, null, null, LoggedUserInfo.DatabasEname);
                }

                Client.SearchBysStoreNameCompleted += (s, sv) =>
                {
                    StoreList = sv.Result;
                };
                Client.GetSmlCompleted += (s, sv) =>
                {
                    foreach (var row in sv.Result)
                    {
                        var newrow = new TblSeasonalMasterListViewModel();

                        newrow.CanEditDeliveryDate = CanChangeDeliveryDate;

                        newrow.InjectFrom(row);
                        newrow.StylePerRow = row.TblStyle1;
                        newrow.ThemePerRow =
                            SelectedStyleViewModel.ThemesList.SingleOrDefault(
                                x => x.Iserial == row.TblSalesOrderColorTheme);
                        newrow.ColorPerRow =
                            SelectedStyleViewModel.ColorList.SingleOrDefault(
                                x => x.Iserial == row.TblColor);
                        var sizeRatioList = new ObservableCollection <TblSeasonalMasterListDetail>();
                        foreach (var sizeCode in SelectedStyleViewModel.Sizes.OrderBy(x => x.Id))
                        {
                            sizeRatioList.Add(new TblSeasonalMasterListDetail
                            {
                                Size = sizeCode.SizeCode,
                            });
                        }
                        //Added By  Hashem to Map with the NEW Stitch System Ratios with Sizes

                        ObservableCollection <TblSeasonalMasterListDetail> data = new ObservableCollection <TblSeasonalMasterListDetail>();

                        if (row.TblSeasonalMasterListDetails.Count() != 0)
                        {
                            //foreach (var r in row.TblSeasonalMasterListDetails)
                            //{
                            foreach (var item in SelectedStyleViewModel.Sizes.OrderBy(x => x.Id))
                            {
                                TblSeasonalMasterListDetail d = new TblSeasonalMasterListDetail();
                                d = row.TblSeasonalMasterListDetails.Where(x => x.Size == item.SizeCode).FirstOrDefault();
                                data.Add(d);
                            }
                            //}
                        }


                        //Commented By Hashem  to Map with the NEW Stitch System Ratios with Sizes
                        //GenericMapper.InjectFromObCollection(newrow.SeasonalMasterListDetail,
                        //row.TblSeasonalMasterListDetails.Count() != 0 ? row.TblSeasonalMasterListDetails : sizeRatioList);

                        GenericMapper.InjectFromObCollection(newrow.SeasonalMasterListDetail,
                                                             row.TblSeasonalMasterListDetails.Count() != 0 ? data : sizeRatioList);
                        try
                        {
                            newrow.DelivaryDate = row.DelivaryDate.Value;
                        }
                        catch { }
                        MainRowList.Add(newrow);
                    }
                    Loading   = false;
                    FullCount = sv.fullCount;
                    if (FullCount == 0 && MainRowList.Count == 0)
                    {
                        AddNewMainRow(false);
                    }
                };

                Client.GetTblStoreIntialOrderCompleted += (s, sv) =>
                {
                    foreach (var row in sv.Result)
                    {
                        var newrow = new TblStoreIntialOrderViewModel();

                        newrow.InjectFrom(row);
                        newrow.StorePerRow = sv.Stores.FirstOrDefault(x => x.iserial == row.Store);
                        SelectedMainRow.DetailList.Add(newrow);
                    }
                    Loading         = false;
                    DetailFullCount = sv.fullCount;
                    if (DetailFullCount == 0 && SelectedMainRow.DetailList.Count == 0)
                    {
                        AddNewDetailRow(false);
                    }
                };

                Client.UpdateOrInsertTblSmlCompleted += (s, x) =>
                {
                    if (x.Error != null)
                    {
                        MessageBox.Show(x.Error.Message);
                    }
                    var savedRow = (TblSeasonalMasterListViewModel)MainRowList.GetItemAt(x.outindex);

                    if (savedRow != null)
                    {
                        savedRow.InjectFrom(x.Result);


                        foreach (var item in x.Result.TblSeasonalMasterListDetails)
                        {
                            var newsizeRow = savedRow.SeasonalMasterListDetail.FirstOrDefault(w => w.Size == item.Size);
                            if (newsizeRow != null)
                            {
                                newsizeRow.Iserial = item.Iserial;
                            }
                        }
                    }
                    Loading = false;
                    if (savedRow.Iserial == 0)
                    {
                        var amountAva    = x.AmountAvaliable;
                        var amountExc    = x.AmountExceeded;
                        var qtyAvaliable = x.QtyAvaliable;
                        var qtyExc       = x.QtyExceeded;

                        MessageBox.Show("Brand Budget Is Not Enough For The current Amounts Or Quantities, Amount Avaliable= " + amountAva + " and  Amount Exceeded= " + amountExc + "Qty Avaliable= " + qtyAvaliable + " and  Qty Exceeded= " + qtyExc);
                    }
                };

                Client.UpdateOrInsertTblStoreIntialOrderCompleted += (s, x) =>
                {
                    var savedRow = SelectedMainRow.DetailList.ElementAt(x.outindex);

                    if (savedRow != null)
                    {
                        savedRow.InjectFrom(x.Result);
                    }
                    Loading = false;
                };
                Client.DeleteSmlCompleted += (s, ev) =>
                {
                    if (ev.Error != null)
                    {
                        throw ev.Error;
                    }

                    var oldrow = MainRowList.FirstOrDefault(x => x.Iserial == ev.Result);
                    if (oldrow != null)
                    {
                        MainRowList.Remove(oldrow);
                    }
                    Loading = false;
                };
                Client.DeleteTblStoreIntialOrderCompleted += (s, ev) =>
                {
                    if (ev.Error != null)
                    {
                        throw ev.Error;
                    }

                    var oldrow = SelectedMainRow.DetailList.FirstOrDefault(x => x.Style == ev.Result.Style && x.Color == ev.Result.Color && x.Store == ev.Result.Store);
                    if (oldrow != null)
                    {
                        SelectedMainRow.DetailList.Remove(oldrow);
                    }
                    Loading = false;
                };
                GetMaindata();
            }
        }