/// <summary>
    /// Creates a new Report template
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnNext_Click(object sender, EventArgs e)
    {
        try
        {
            ReportTemplate.ReportType type = (ReportTemplate.ReportType)int.Parse(Request.QueryString["reportType"]);
            ReportTemplate template = null;
            Template templateRow = new Template();
            templateRow.TemplateName = "New Template";

            switch (type)
            {
                case ReportTemplate.ReportType.GroceryRescue:
                    {
                        templateRow.TemplateType = (int)ReportTemplate.ReportType.GroceryRescue;
                        template = new GroceryRescueReportTemplate();
                        break;
                    }
                case ReportTemplate.ReportType.InOut:
                    {
                        templateRow.TemplateType = (int)ReportTemplate.ReportType.InOut;
                        template = new InOutReportTemplate();
                        break;
                    }
                case ReportTemplate.ReportType.Inventory:
                    {
                        templateRow.TemplateType = (int)ReportTemplate.ReportType.Inventory;
                        template = new  InventoryReportTemplate();
                        break;
                    }
                case ReportTemplate.ReportType.Incoming:
                    {
                        templateRow.TemplateType = (int)ReportTemplate.ReportType.Incoming;
                        template = new IncomingReportTemplate();
                        break;
                    }
                case ReportTemplate.ReportType.Outgoing:
                    {
                        templateRow.TemplateType = (int)ReportTemplate.ReportType.Outgoing;
                        template = new OutgoingReportTemplate();
                        break;
                    }
            }

            Session["reportTemplateRow"] = templateRow;
            Session["reportTemplate"] = template;
            NextPage(type);
        }
        catch (System.Threading.ThreadAbortException) { }
        catch (Exception ex)
        {
            LogError.logError(ex);
            Response.Redirect("~/errorpages/error.aspx");
        }
    }
    /// <summary>
    /// Loads the data into a dataset depending on the options set by the user.
    /// </summary>
    /// <param name="startdate">The date to start from</param>
    /// <param name="enddate">The date to end grabbing transactions</param>
    /// <param name="template">The template to load options for the query</param>
    /// <returns>The dataset with the data loaded into it</returns>
    private ReportsDataSet LoadData(DateTime startdate, DateTime enddate, InOutReportTemplate template)
    {
        ReportsDataSet ds = new ReportsDataSet();

        using (CCSEntities db = new CCSEntities())
        {
            DateTime dt = enddate.AddDays(1);
            List<FoodCategory> foodCategoriesData = null;
            List<USDACategory> usdaCategoriesData = null;

            foodCategoriesData = GenerateDatasetHelper.GetFoodCategoriesBySelection(template, db);

            if (template.USDASelection == ReportTemplate.SelectionType.ALL)
            {
                usdaCategoriesData = db.USDACategories.OrderBy(f => f.Description).ToList();
            }
            else if (template.USDASelection == ReportTemplate.SelectionType.SOME)
            {
                List<short> selectedUSDA = new List<short>();
                foreach (var i in template.USDACategories)
                    selectedUSDA.Add(short.Parse(i));

                usdaCategoriesData = (from u in db.USDACategories
                                      where  selectedUSDA.Contains(u.USDAID)
                                      orderby u.Description
                                      select u).ToList();
            }

            if (template.CategoriesSelection != ReportTemplate.SelectionType.NONE)
            {
                foreach (var f in foodCategoriesData)
                {

                    double weightIn = (from w in f.FoodIns
                                       where (w.TimeStamp >= startdate.Date && w.TimeStamp < dt.Date)
                                        select (double)w.Weight).Sum();

                    double weightOut = (from w in f.FoodOuts
                                        where (w.TimeStamp >= startdate.Date && w.TimeStamp < dt.Date)

                                        select w.Weight).Sum();

                    double countIn = (from w in f.FoodIns
                                      where (w.TimeStamp >= startdate.Date && w.TimeStamp < dt.Date)
                                          && w.Count != null
                                      select (double)w.Count).Sum();

                    double countOut = (from w in f.FoodOuts
                                       where (w.TimeStamp >= startdate.Date && w.TimeStamp < dt.Date)
                                           && w.Count != null
                                       select (double)w.Count).Sum();

                    double currentBalanceWeight = 0;
                    double currentBalanceCount = 0;

                    if (template.ShowCurrentBalance)
                    {
                        currentBalanceWeight = (from c in f.Containers
                                          select (double)c.Weight).Sum();
                        currentBalanceCount = (from c in f.Containers
                                                where c.Cases != null
                                                select (double)c.Cases).Sum();
                    }
                    ds.InOut.AddInOutRow(f.CategoryType, false, currentBalanceWeight, currentBalanceCount, weightIn, weightOut, countIn, countOut);
                }
            }

            if (template.USDASelection != ReportTemplate.SelectionType.NONE)
            {
                foreach (var f in usdaCategoriesData)
                {

                    double weightIn = (from w in f.FoodIns
                                       where w.TimeStamp < dt.Date && w.TimeStamp >= startdate
                                       select (double)w.Weight).Sum();

                    double weightOut = (from w in f.FoodOuts
                                        where w.TimeStamp < dt.Date && w.TimeStamp >= startdate
                                        select w.Weight).Sum();

                    double countIn = (from w in f.FoodIns
                                      where w.TimeStamp < dt.Date && w.TimeStamp >= startdate && w.Count != null
                                      select (double)w.Count).Sum();

                    double countOut = (from w in f.FoodOuts
                                       where w.TimeStamp < dt.Date && w.TimeStamp >= startdate && w.Count != null
                                       select (double)w.Count).Sum();

                    double currentBalanceWeight = 0;
                    double currentBalanceCount = 0;

                    if (template.ShowCurrentBalance)
                    {
                        currentBalanceWeight = (from c in f.Containers
                                                select (double)c.Weight).Sum();
                        currentBalanceCount = (from c in f.Containers
                                               where c.Cases != null
                                               select (double)c.Cases).Sum();
                    }
                    ds.InOut.AddInOutRow("(" + f.USDANumber + ")" + f.Description, true, currentBalanceWeight, currentBalanceCount, weightIn, weightOut, countIn, countOut);
                }
            }
        }
        return ds;
    }