private string CreateHtmlTable(IEnumerable <PrintEntry> entries)
        {
            StringBuilder table = new StringBuilder();

            string style = @"<style> @media print{thead{display: table-header-group;}} img { height:100px; max-width:100px; width: expression(this.width > 500 ? 500: true);} td { text-align: center;} .input { padding: 50px; }  </style>";

            table.AppendFormat(@"<html><head>{0}</head><body><table border=""2"">", style);

            PictureSetRepository picRepository = new PictureSetRepository();

            var classGroups = entries.GroupBy(p => p.ClassName).OrderBy(p => p.Key);

            table.Append("<thead><tr><th>Picture</th><th>Brand</th><th>Gender</th><th>Sku</th><th>Qty</th><th>Cost</th><th>UPC</th><th>Active</th><th>ORI</th><th>STG</th><th>OMS</th><th>LCS</th></tr></thead><tbody>");

            foreach (var classGroup in classGroups)
            {
                var pics = picRepository.GetPictures(classGroup.First().Brand, classGroup.Select(p => p.Sku).ToList());

                var mainPic = pics.OrderBy(p => p.Name).FirstOrDefault();

                bool hasImage = false;

                string rowSpan = classGroup.Count().ToString();

                foreach (PrintEntry entry in classGroup.OrderBy(p => p.Sku))
                {
                    table.Append("<tr>");

                    if (!hasImage)
                    {
                        if (mainPic != null)
                        {
                            table.AppendFormat(@"<td rowspan=""{1}""><img src=""{0}""></td>", mainPic.Path, rowSpan);
                        }
                        else
                        {
                            table.AppendFormat(@"<td rowspan=""{1}""><img src=""{0}""></td>", string.Empty, rowSpan);
                        }


                        table.AppendFormat(@"<td rowspan=""{1}"">{0}</td>", entry.Brand, rowSpan);
                        table.AppendFormat(@"<td rowspan=""{1}"">{0}</td>", entry.Gender, rowSpan);
                    }


                    table.AppendFormat("<td>{0}</td>", entry.Sku);
                    table.AppendFormat("<td>{0}</td>", entry.Qty);
                    table.AppendFormat("<td>{0:C}</td>", entry.Cost);



                    table.AppendFormat("<td>{0}</td>", string.IsNullOrEmpty(entry.UPC) ? "No" : "Yes");

                    table.AppendFormat(@"<td class=""history"">{0}</td>", entry.Active);


                    if (!hasImage)
                    {
                        table.AppendFormat(@"<td class=""input"" rowspan=""{0}"">     </td>", rowSpan);
                        table.AppendFormat(@"<td class=""input""  rowspan=""{0}"">     </td>", rowSpan);
                        table.AppendFormat(@"<td class=""input""  rowspan=""{0}"">     </td>", rowSpan);
                        table.AppendFormat(@"<td class=""input""  rowspan=""{0}"">     </td>", rowSpan);

                        hasImage = true;
                    }

                    table.Append("</tr>");
                }
            }

            table.Append("</tbody></table></body></html>");

            return(table.ToString());
        }
        public void UpdateMainEntries(IEnumerable <MainEntry> entries)
        {
            var                  titleMaps     = _workbook.ReadTitleMapRules();
            TextInfo             textInfo      = Thread.CurrentThread.CurrentCulture.TextInfo;
            PictureSetRepository picRepository = new PictureSetRepository();

            using (berkeleyEntities dataContext = new berkeleyEntities())
            {
                dataContext.MaterializeAttributes = true;

                foreach (MainEntry entry in entries)
                {
                    try
                    {
                        Item item = dataContext.Items.Include("EbayListingItems").Include("AmznListingItems").SingleOrDefault(p => p.ItemLookupCode.Equals(entry.Sku));

                        if (item == null)
                        {
                            entry.Message = "sku not found";
                            continue;
                        }

                        entry.Brand     = item.SubDescription1;
                        entry.ClassName = item.ClassName;
                        entry.Qty       = item.QtyAvailable;
                        entry.Cost      = item.Cost;

                        entry.Department  = item.DepartmentName;
                        entry.Category    = item.CategoryName;
                        entry.Gender      = item.SubDescription3;
                        entry.Color       = item.SubDescription2;
                        entry.Notes       = item.Notes;
                        entry.Price       = item.Price;
                        entry.Location    = item.BinLocation;
                        entry.Cost        = item.Cost;
                        entry.Qty         = item.QtyAvailable;
                        entry.Description = item.Description;
                        entry.UPC         = item.GTIN;

                        var pics = picRepository.GetPictures(entry.Brand, new List <string>()
                        {
                            entry.Sku
                        });

                        entry.PictureCount = pics.Count;

                        var titleMap = titleMaps.SingleOrDefault(p => p.Department.Equals(item.DepartmentName) && p.Category.Equals(item.CategoryName));

                        if (titleMap == null)
                        {
                            titleMap     = new TitleMapRule();
                            titleMap.Map = "";
                        }

                        string description = item.Description;
                        string dims        = string.Empty;

                        foreach (var attribute in item.Dimensions)
                        {
                            description = description.Replace(" " + attribute.Value.Value + " ", "");
                            dims       += attribute.Value.Value + " ";
                        }

                        entry.TitleFormula = textInfo.ToTitleCase((entry.Brand + " " + titleMap.Map + " Size " + dims + description).ToLower());

                        var ebayHistory = string.Join(" ", item.EbayListingItems.Where(p => p.Listing.Status.Equals(EbayMarketplace.STATUS_ACTIVE)).Select(p => p.ToString()));
                        var amznHistory = string.Join(" ", item.AmznListingItems.Where(p => p.IsActive).Select(p => p.ToString()));

                        entry.Status = ebayHistory + " " + amznHistory;

                        if (item.EbayListingItems.Where(w => w.Listing.IsVariation.HasValue && !w.Listing.IsVariation.Value).Count() > 0)
                        {
                            EbayListingItem listingItem = item.EbayListingItems.Single(p => p.ID == item.EbayListingItems.Where(w => w.Listing.IsVariation.HasValue && !w.Listing.IsVariation.Value).Max(s => s.ID));
                            entry.Title           = listingItem.Listing.Title;
                            entry.FullDescription = listingItem.Listing.FullDescription;
                        }
                        else if (dataContext.bsi_quantities.Any(p => p.itemLookupCode.Equals(entry.Sku)))
                        {
                            var postDetails = dataContext.bsi_quantities.Where(p => p.itemLookupCode.Equals(entry.Sku));

                            int lastPostDetailID = postDetails.Max(p => p.id);

                            bsi_quantities postDetail = postDetails.Single(p => p.id == lastPostDetailID);

                            entry.Title           = postDetail.title;
                            entry.FullDescription = postDetail.bsi_posts.bsi_posting.fullDescription;
                        }
                    }
                    catch (Exception e)
                    {
                        entry.Message = e.Message;
                    }
                }
            }
        }