public void FillGeneralFollowUpsWithCompany()
        {
            GeneralFollowUps.Clear();

            var products = from d in db.CostProduct
                           select d;

            GeneralFollowUp gfu = new GeneralFollowUp();

            DateTime date = DateTime.ParseExact("20000101", "yyyyMMdd", CultureInfo.InvariantCulture);

            foreach (var item in products)
            {
                gfu.Costs += item.CeAmount;

                if (item.CeIncomeDate > date)
                {
                    date = item.CeIncomeDate;
                }
            }
            gfu.ObjectName = "Företag";
            gfu.ObjectID   = "FÖ";
            gfu.Date       = date;

            GeneralFollowUps.Add(gfu);
        }
        public void FillGeneralFollowUpsWithProductGroups()
        {
            GeneralFollowUps.Clear();

            var tempGroupsAdded = new List <string>();

            var products = from d in db.Product
                           select d;

            var cps = from c in db.CostProduct
                      select c;

            foreach (var product in products)
            {
                foreach (var cp in cps)
                {
                    if (product.ProductID.Equals(cp.CeProductID))
                    {
                        GeneralFollowUp gfu = new GeneralFollowUp();
                        gfu.ObjectID   = product.ProductGroupID;
                        gfu.ObjectName = product.ProductGroup.ProductGroupName;
                        gfu.Date       = cp.CeIncomeDate;

                        // Prevent same department from being added more than once
                        if (!tempGroupsAdded.Contains(product.ProductGroupID))
                        {
                            GeneralFollowUps.Add(gfu);
                        }
                        tempGroupsAdded.Add(product.ProductGroupID);
                    }
                }
            }
        }
        public void FillGeneralFollowUpsWithProducts()
        {
            GeneralFollowUps.Clear();

            foreach (var item in db.CostProduct)
            {
                GeneralFollowUp gfu = new GeneralFollowUp
                {
                    ObjectID   = item.CeProductID,
                    ObjectName = item.CeProductName,
                    Date       = item.CeIncomeDate
                };

                GeneralFollowUp gfuToRemove = null;

                foreach (var tempGFU in GeneralFollowUps
                         .Where(tempGFU => tempGFU.ObjectID.Equals(gfu.ObjectID) && tempGFU.Date <= gfu.Date))
                {
                    gfuToRemove = tempGFU;
                }

                if (gfuToRemove != null)
                {
                    GeneralFollowUps.Remove(gfuToRemove);
                }

                GeneralFollowUps.Add(gfu);
            }
        }
        public void FillGeneralFollowUpsWithCompany()
        {
            GeneralFollowUps.Clear();
            GeneralFollowUp gfu = new GeneralFollowUp {
                ObjectName = "IT-Service"
            };

            GeneralFollowUps.Add(gfu);
        }
        public GeneralFollowUp GetResults(CostProductOption cpo, string objectID)
        {
            GeneralFollowUp gfu = new GeneralFollowUp();

            switch (cpo)
            {
            case CostProductOption.Product:
                var product = db.Product.Single(p => p.ProductID.Equals(objectID));
                gfu.ObjectName = product.ProductName;

                gfu.Costs    = (int)GetDirectProductCostByProductID(objectID);
                gfu.Revenues = GetCalculatedIncomeBudgetByProduct(objectID);

                gfu.Result = gfu.Revenues - gfu.Costs;
                break;

            case CostProductOption.Productgroup:
                var productGroup = db.ProductGroup.Single(p => p.ProductGroupID.Equals(objectID));
                gfu.ObjectName = productGroup.ProductGroupName;

                gfu.Costs    = GetProductGroupCostByID(objectID);
                gfu.Revenues = GetCalculatedIncomeBudgetByGroup(objectID);

                gfu.Result = gfu.Revenues - gfu.Costs;
                break;

            case CostProductOption.Department:
                var department = db.Department.Single(p => p.DepartmentID.Equals(objectID));
                gfu.ObjectName = department.DepartmentName;

                if (objectID == "DA" || objectID == "UF")
                {
                    gfu.Costs    = GetProductionDepartmentCostByDepartmentID(objectID);
                    gfu.Revenues = GetCalculatedIncomeBudgetProductByDepartment(objectID);
                }
                if (objectID == "AO" || objectID == "FO")
                {
                    gfu.Costs    = GetAFFODepartmentCostByDepartmentID(objectID);
                    gfu.Revenues = 0;
                }

                gfu.Result = gfu.Revenues - gfu.Costs;
                break;

            case CostProductOption.Company:
                gfu.ObjectName = "IT-Service";

                gfu.Costs    = (int)GetTotalCost();
                gfu.Revenues = GetCalculatedTotalIncomeBudget();

                gfu.Result = gfu.Revenues - gfu.Costs;
                break;
            }

            return(gfu);
        }
        public GeneralFollowUp GetResults(CostProductOption cpo, GeneralFollowUp inGFU)
        {
            GeneralFollowUp gfu = new GeneralFollowUp();

            var products = from p in CostProducts
                           where p.CeProductID.Equals(inGFU.ObjectID)
                           orderby p.CeIncomeDate descending
                           select p;

            CostProduct product = null;

            var pg = from p in ProductManagement.Instance.Products
                     where p.ProductGroupID.Equals(inGFU.ObjectID)
                     select p;

            var pd = from p in ProductManagement.Instance.Products
                     where p.DepartmentID.Equals(inGFU.ObjectID)
                     select p;

            var cps = from cp in CostProducts
                      orderby cp.CeIncomeDate descending
                      select cp;

            Product productToLookFor = null;


            switch (cpo)
            {
            case CostProductOption.Product:

                if (products.Any())
                {
                    product = products.First(s => s.CeProductID.Equals(inGFU.ObjectID));
                }

                if (product != null)
                {
                    var revenues = from c in db.IncomeProductCustomer
                                   where c.IeProductID.Equals(product.CeProductID)
                                   orderby c.IeAmount ascending
                                   select c;

                    var costs = from r in db.CostProduct
                                where r.CeProductID.Equals(product.CeProductID)
                                orderby r.CeAmount descending
                                select r;

                    int revenue = 0, cost = 0;

                    if (revenues.Any())
                    {
                        var p = revenues.First();
                        revenue = ~p.IeAmount + 1;
                    }

                    if (costs.Any())
                    {
                        var re = costs.First();
                        cost = re.CeAmount;
                    }

                    gfu.ObjectName = product.CeProductName;
                    gfu.Costs      = cost;
                    gfu.Revenues   = revenue;
                    gfu.Result     = gfu.Revenues - gfu.Costs;
                    gfu.Date       = inGFU.Date;
                }
                break;

            case CostProductOption.Productgroup:


                foreach (var p in pg.Where(p => p.ProductGroupID.Equals(inGFU.ObjectID)))
                {
                    productToLookFor = p;
                }

                if (cps.Any() && productToLookFor != null)
                {
                    gfu.Date = cps.First(s => s.CeProductID.Equals(productToLookFor.ProductID)).CeIncomeDate;
                }

                gfu.Revenues = GetProductGroupRevenues(productToLookFor.ProductID);
                gfu.Costs    = GetProductGroupCosts(productToLookFor.ProductID);

                var productGroup = db.ProductGroup.Single(p => p.ProductGroupID.Equals(inGFU.ObjectID));
                gfu.ObjectName = productGroup.ProductGroupName;

                gfu.Result = gfu.Revenues - gfu.Costs;

                break;

            case CostProductOption.Department:

                foreach (var p in from p in pd.Where(p => p.DepartmentID.Equals(inGFU.ObjectID))
                         from item in cps
                         where item.CeProductID.Equals(p.ProductID)
                         select p)
                {
                    productToLookFor = p;
                }

                if (cps.Any() && productToLookFor != null)
                {
                    gfu.Date = cps.First(s => s.CeProductID.Equals(productToLookFor.ProductID)).CeIncomeDate;
                }


                gfu.Costs    = GetProductGroupCosts(productToLookFor.ProductID);
                gfu.Revenues = GetProductGroupRevenues(productToLookFor.ProductID);

                var department = db.Department.First(p => p.DepartmentID.Equals(inGFU.ObjectID));
                gfu.ObjectName = department.DepartmentName;

                gfu.Result = gfu.Revenues - gfu.Costs;
                break;

            case CostProductOption.Company:
                gfu.ObjectName = "IT-Service";


                foreach (var item in db.CostProduct)
                {
                    gfu.Costs += item.CeAmount;
                }


                foreach (var item in db.IncomeProductCustomer)
                {
                    gfu.Revenues += ~item.IeAmount + 1;
                }

                gfu.Date   = inGFU.Date;
                gfu.Result = gfu.Revenues - gfu.Costs;
                break;
            }

            return(gfu);
        }