public IHttpActionResult PostSingleLinearForcast(SingleLinearForcast singleLinearForcast)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.SingleLinearForcasts.Add(singleLinearForcast);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (SingleLinearForcastExists(singleLinearForcast.SKU))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = singleLinearForcast.SKU }, singleLinearForcast);
        }
        public IHttpActionResult PutSingleLinearForcast(int id, SingleLinearForcast singleLinearForcast)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != singleLinearForcast.SKU)
            {
                return BadRequest();
            }

            db.Entry(singleLinearForcast).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SingleLinearForcastExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
 public static void GenerateSingleLinearForcast(int id)
 {
     ApplicationDbContext db = new ApplicationDbContext();
     MonthlyTotal MT = db.MonthlyTotals.Where(p => p.SKU == id).OrderBy(g => g.Date).ToArray().Last();
     DateTime ForcastDate = MT.Date.AddMonths(1);
     int forcast_month_id = (12 * ForcastDate.Year) + ForcastDate.Month;
     List<MonthlyTotal> Totals = db.MonthlyTotals.Where(P => P.SKU == id && P.Date < ForcastDate).OrderByDescending(O => O.Date).Take(12).ToList();
     List<LinearDataSet> DS = new List<LinearDataSet>();
     double Intercept = 0;
     double Slope = 0;
     double Absolute_Quantity_Forcast = 0;
     double Quantity_Forcast = 0;
     string JSON_MonthlyTotals = JsonConvert.SerializeObject(DS.ToArray());
     int Sample_Size = Totals.Count();
     string SkuClass = "Unknown";
     bool Valid = false;
     if (Sample_Size > 3)
     {
         Valid = true;
         double[] YTotals = Totals.Select(P => P.Absolute_Quantity_Sold).ToArray();
         double[] YTotalsR = Totals.Select(P => P.Quantity_Sold).ToArray();
         double[] XMonth = Totals.Select(P => (double)(P.Date.Year * 12) + P.Date.Month).ToArray();
         double[] t = Linear.Forcast(forcast_month_id, XMonth, YTotals);
         DS.Add(new LinearDataSet() { label = "Actual Sales", y = YTotalsR, x = XMonth });
         DS.Add(new LinearDataSet() { label = "Adjusted Sales", y = YTotals, x = XMonth });
         List<double> TrendYVals = new List<double>();
         List<double> TrendYValsSeasonal = new List<double>();
         foreach (double x in XMonth)
         {
             double val = x * t[1] + t[0];
             TrendYVals.Add(val);
         }
         DS.Add(new LinearDataSet()
         {
             label = "Trend Sales",
             y = TrendYVals.ToArray(),
             x = XMonth
         });
         JSON_MonthlyTotals = JsonConvert.SerializeObject(DS.ToArray());
         Intercept = t[0];
         Slope = t[1];
         Absolute_Quantity_Forcast = t[2];
         Quantity_Forcast = MonthlyTotal.AddSeasonality(Absolute_Quantity_Forcast, ForcastDate.Month);
         SkuClass = SingleLinearForcast.GetSkuClass((int)Totals.Select(P => P.Quantity_Sold).ToArray().Sum());
     }
     SingleLinearForcast Forcast = new SingleLinearForcast()
     {
         SKU = id,
         Month_Id = (12 * ForcastDate.Year) + ForcastDate.Month, //Month * Year
         Date = ForcastDate, //Forcasted Date
         Quantity_Forcast = Quantity_Forcast,
         Absolute_Quantity_Forcast = Absolute_Quantity_Forcast,
         Slope = Slope,
         Intercept = Intercept,
         JSON_MonthlyTotals = JSON_MonthlyTotals,
         Sample_Size = Sample_Size,
         SkuClass = SkuClass,
         Valid = Valid,
     };
     db.SingleLinearForcasts.Add(Forcast);
     db.SaveChanges();
 }