/// <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 dataset with the data loaded into it</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, OutgoingReportTemplate template)
    {
        ReportsDataSet ds = new ReportsDataSet();

        Console.WriteLine(template.ToString());

        using (CCSEntities db = new CCSEntities())
        {

            DateTime dt = enddate.AddDays(1);
            List<FoodOut> data = db.FoodOuts.Where(f => (f.TimeStamp >= startdate.Date && f.TimeStamp < dt.Date)).ToList();

            if (template.FoodSourceTypesSelection == ReportTemplate.SelectionType.SOME)
            {
                List<short> selectedTypes = new List<short>();
                foreach (var i in template.FoodSourceTypes)
                    selectedTypes.Add(short.Parse(i));

                data = (from c in data
                        where selectedTypes.Contains((short)c.FoodSourceTypeID)
                        select c).ToList();
            }

            if (template.DistributionSelection == ReportTemplate.SelectionType.SOME)
            {
                List<short> selectedDistribution = new List<short>();
                foreach (var i in template.DistributionTypes)
                    selectedDistribution.Add(short.Parse(i));

                data = (from c in data
                        where selectedDistribution.Contains((short)c.DistributionTypeID)
                        select c).ToList();
            }

            if (template.AgenciesSelection == ReportTemplate.SelectionType.SOME)
            {
                List<short> selectedAgencies = new List<short>();
                foreach (var i in template.Agencies)
                    selectedAgencies.Add(short.Parse(i));

                data = (from c in data
                        where selectedAgencies.Contains((short)c.AgencyID)
                        select c).ToList();
            }

            List<FoodOut> foodInRegularData = new List<FoodOut>();

            List<FoodOut> foodInUSDAData = new List<FoodOut>();

            if (template.CategoriesSelection != ReportTemplate.SelectionType.NONE)
                foodInRegularData = data.Where(f => f.FoodCategory != null).ToList();

            if (template.USDASelection != ReportTemplate.SelectionType.NONE)
                foodInUSDAData = data.Where(f => f.USDACategory != null).ToList();

            if (template.CategoriesSelection == ReportTemplate.SelectionType.REGULAR)
            {
                foodInRegularData = (from c in foodInRegularData
                                     where c.FoodCategory.Perishable == false && c.FoodCategory.NonFood == false
                                     select c).ToList();
            }
            else if (template.CategoriesSelection == ReportTemplate.SelectionType.PERISHABLE)
            {
                foodInRegularData = (from c in foodInRegularData
                                     where c.FoodCategory.Perishable == true && c.FoodCategory.NonFood == false
                                     select c).ToList();
            }
            else if (template.CategoriesSelection == ReportTemplate.SelectionType.NONFOOD)
            {
                foodInRegularData = (from c in foodInRegularData
                                     where c.FoodCategory.Perishable == false && c.FoodCategory.NonFood == true
                                     select c).ToList();
            }
            else if (template.CategoriesSelection == ReportTemplate.SelectionType.SOME)
            {
                List<short> selectedCategories = new List<short>();
                foreach (var i in template.FoodCategories)
                    selectedCategories.Add(short.Parse(i));

                foodInRegularData = (from c in foodInRegularData
                                     where selectedCategories.Contains((short)c.FoodCategoryID)
                                     select c).ToList();
            }

            if (template.USDASelection == ReportTemplate.SelectionType.SOME)
            {
                List<short> selectedUSDA = new List<short>();
                foreach (var i in template.USDACategories)
                    selectedUSDA.Add(short.Parse(i));

                foodInUSDAData = (from u in foodInUSDAData
                                  where selectedUSDA.Contains((short)u.USDAID)
                                  select u).ToList();
            }

            foodInRegularData.InsertRange(0, foodInUSDAData);

            data = foodInRegularData;

            /**
            *       @Author Jake Abel
            *
            *       Sort the results based on FoodSource Type, Distribution type, and then based on Agency.
            */

            // Static variables of what they want to come first.
            const string taxable = "In-Kind (Taxable)";
            const string nonTaxable = "In-Kind (Non-Tax)";
            const string noAgency = "No-Agency";

            data.Sort(delegate(FoodOut dis, FoodOut otr)
            {

                // Put the taxable first, and then the non taxable, and  then whatever
                if (dis.FoodSourceType.FoodSourceType1.Equals(taxable) || dis.FoodSourceType.FoodSourceType1.Equals(nonTaxable) ||
                    otr.FoodSourceType.FoodSourceType1.Equals(taxable) || otr.FoodSourceType.FoodSourceType1.Equals(nonTaxable))
                {
                    if (dis.FoodSourceType.FoodSourceType1.Equals(taxable) && !otr.FoodSourceType.FoodSourceType1.Equals(taxable))
                    {
                        return -1;
                    }

                    if (otr.FoodSourceType.FoodSourceType1.Equals(taxable) && !dis.FoodSourceType.FoodSourceType1.Equals(taxable))
                    {
                        return 1;
                    }

                    if (dis.FoodSourceType.FoodSourceType1.Equals(nonTaxable) && !otr.FoodSourceType.FoodSourceType1.Equals(nonTaxable))
                    {
                        return -1;
                    }

                    if (otr.FoodSourceType.FoodSourceType1.Equals(nonTaxable) && !dis.FoodSourceType.FoodSourceType1.Equals(nonTaxable))
                    {
                        return 1;
                    }

                }

                if (dis.FoodSourceType.FoodSourceType1.Contains(taxable) && !otr.FoodSourceType.FoodSourceType1.Contains(taxable))
                {
                    return 1;
                }
                if (dis.FoodSourceType.FoodSourceType1.Contains(nonTaxable) && !otr.FoodSourceType.FoodSourceType1.Contains(nonTaxable))
                {
                    return 1;
                }

                // Sorting based on distribution type, if they are the same, continue
                int ret = dis.DistributionType.DistributionType1.CompareTo(otr.DistributionType.DistributionType1);
                if (ret != 0)
                {
                    return ret;
                }

                // Sort based on agency very last of all
                if (dis.Agency == null && otr.Agency == null)
                {
                    return 0;
                }
                else if (dis.Agency == null)
                {
                    return noAgency.CompareTo(otr.Agency.AgencyName);
                }
                else if (otr.Agency == null)
                {
                    return dis.Agency.AgencyName.CompareTo(noAgency);
                }
                else
                {
                    return dis.Agency.AgencyName.CompareTo(otr.Agency.AgencyName);
                }
            });

            // Original Version @Author Nittaya Phonharath
            //            foreach (var i in data)
            //            {
            //                if(i.FoodCategory != null || i.USDACategory != null)
            //                    ds.Outgoing.AddOutgoingRow(i.FoodCategory == null? i.USDACategory.Description: i.FoodCategory.CategoryType, i.BinNumber, i.TimeStamp, (double)(i.Count ?? 0), i.Weight, i.Agency == null ? "No-Agency" : i.Agency.AgencyName, i.DistributionType.DistributionType1, i.FoodSourceType.FoodSourceType1);
            //            }

            // @Author Jake Abel
            // Modified version, used for cleanliness and readability

            foreach (var i in data)
            {

        //              FoodCategory foodCategory = i.FoodCategory;
                if (i.FoodCategory != null || i.USDACategory != null)
                {
                    //ds.Outgoing.AddOutgoingRow(i.FoodCategory == null? i.USDACategory.Description: i.FoodCategory.CategoryType, i.BinNumber, i.TimeStamp, (double)(i.Count ?? 0),
                    //i.Weight, i.Agency == null ? "No-Agency" : i.Agency.AgencyName, i.DistributionType.DistributionType1, i.FoodSourceType.FoodSourceType1);

                    string foodCategory = "";
                    if (i.FoodCategory == null)
                    {
                        foodCategory = i.USDACategory.Description;
                    }
                    else
                    {
                        foodCategory = i.FoodCategory.CategoryType;
                    }

                    short binNumber = i.BinNumber;
                    DateTime timeStamp = i.TimeStamp;
                    short count;
                    if (i.Count == null)
                    {
                        count = 0;
                    }
                    else
                    {
                        count = (short)i.Count;
                    }

                    double weight = i.Weight;

                    string agencyName = "";

                    if (i.Agency == null)
                    {
                        agencyName = "No-Agency";
                    }
                    else
                    {
                        agencyName = i.Agency.AgencyName;
                    }

                    string distributionType = i.DistributionType.DistributionType1;
                    string foodSourceType2 = i.FoodSourceType.FoodSourceType1;

                    ds.Outgoing.AddOutgoingRow(foodCategory, binNumber, timeStamp, count, weight, agencyName, distributionType, foodSourceType2);
                }

            }

        }
        return ds;
    }