// Checks if a dataset with given name exists and returns it if it does.
        // Otherwise, creates a new data set.
        public AssetDataSet OpenDataSet(string name)
        {
            var item = GetExistingItemWithType <AssetDataSet>(name);

            if (item != null)
            {
                return(item);
            }

            var dataset = new AssetDataSet(m_Path, name, authorId);

            m_Items.Add(dataset);
            return(dataset);
        }
Ejemplo n.º 2
0
        public AssetDepreciationPlan CalculateAssetDepreciationPlan(AssetDataSet assetDataSet, Asset asset, bool plan)
        {
            Dictionary <int, int> years = new Dictionary <int, int>();
            int yearIndex = -1;
            AssetDepreciationPlan    assetDepreciationPlan = new AssetDepreciationPlan();
            DepreciationCalculations depCalc = new DepreciationCalculations();
            int fistDepMonth = depCalc.CalculateNextDepreciationMonth(asset.StartUsingDate);
            int fistDepYear  = depCalc.CalculateNextDepreciationYear(asset.StartUsingDate);
            int fistMonth    = asset.StartUsingDate.Month;
            int fistYear     = asset.StartUsingDate.Year;
            int no           = 0;

            assetDepreciationPlan.StartMonth           = assetDataSet.MonthNames[fistDepMonth] + " " + fistDepYear.ToString();
            assetDepreciationPlan.TotalRemainingAmount = asset.InitialValue;
            assetDepreciationPlan.AssetName            = asset.AssetName;
            assetDepreciationPlan.DepreciationRate     = assetDataSet.DepreciationTypes[asset.DepreciationTypeId].Name;
            assetDepreciationPlan.InitialValue         = asset.InitialValue;
            assetDepreciationPlan.YearCharge           = decimal.Round((asset.InitialValue * (assetDataSet.DepreciationTypes[asset.DepreciationTypeId].DepreciationRate / 100)) / 12, 2) * (decimal)12;
            assetDepreciationPlan.MonthlyCharge        = decimal.Round((asset.InitialValue * (assetDataSet.DepreciationTypes[asset.DepreciationTypeId].DepreciationRate / 100)) / 12, 2);

            foreach (DepreciationCharge depreciationCharge in assetDataSet.DepreciationCharges)
            {
                if (!years.ContainsKey(depreciationCharge.Year))
                {
                    yearIndex++;
                    years.Add(depreciationCharge.Year, depreciationCharge.Year);
                    AssetDepreciationYearPlan  assetDepreciationYearPlan  = new AssetDepreciationYearPlan();
                    AssetDepreciationMonthPlan assetDepreciationMonthPlan = new AssetDepreciationMonthPlan();

                    assetDepreciationMonthPlan.No                 = depreciationCharge.No;
                    assetDepreciationMonthPlan.MonthYear          = assetDataSet.MonthNames[depreciationCharge.Month] + " " + depreciationCharge.Year.ToString();
                    assetDepreciationMonthPlan.CurrentCharge      = depreciationCharge.CurrentCharge;
                    assetDepreciationMonthPlan.CumulativelyCharge = depreciationCharge.CumulativelyCharge;
                    assetDepreciationMonthPlan.RemainingAmount    = depreciationCharge.RemainingAmount;

                    assetDepreciationYearPlan.TotalYearCharge += depreciationCharge.CurrentCharge;
                    assetDepreciationYearPlan.Year             = depreciationCharge.Year;
                    assetDepreciationYearPlan.AssetDepreciationMonthPlans.Add(assetDepreciationMonthPlan);

                    assetDepreciationPlan.AssetDepreciationYearPlans.Add(assetDepreciationYearPlan);
                }
                else
                {
                    AssetDepreciationYearPlan  assetDepreciationYearPlan  = assetDepreciationPlan.AssetDepreciationYearPlans[yearIndex];
                    AssetDepreciationMonthPlan assetDepreciationMonthPlan = new AssetDepreciationMonthPlan();

                    assetDepreciationMonthPlan.No                 = depreciationCharge.No;
                    assetDepreciationMonthPlan.MonthYear          = assetDataSet.MonthNames[depreciationCharge.Month] + " " + depreciationCharge.Year.ToString();
                    assetDepreciationMonthPlan.CurrentCharge      = depreciationCharge.CurrentCharge;
                    assetDepreciationMonthPlan.CumulativelyCharge = depreciationCharge.CumulativelyCharge;
                    assetDepreciationMonthPlan.RemainingAmount    = depreciationCharge.RemainingAmount;

                    assetDepreciationYearPlan.TotalYearCharge += depreciationCharge.CurrentCharge;

                    assetDepreciationYearPlan.AssetDepreciationMonthPlans.Add(assetDepreciationMonthPlan);
                }

                assetDepreciationPlan.TotalCurrentCharge      += depreciationCharge.CurrentCharge;
                assetDepreciationPlan.TotalCumulativelyCharge += depreciationCharge.CurrentCharge;
                assetDepreciationPlan.TotalRemainingAmount    -= depreciationCharge.CurrentCharge;

                fistMonth = depreciationCharge.Month;
                fistYear  = depreciationCharge.Year;
                no        = depreciationCharge.No;
            }

            if (!plan || assetDepreciationPlan.TotalCumulativelyCharge == asset.InitialValue)
            {
                return(assetDepreciationPlan);
            }


            bool next = true;

            while (next)
            {
                fistMonth++;
                no++;
                if (fistMonth > 12)
                {
                    fistMonth = 1;
                    fistYear++;
                }
                decimal depreciation = assetDepreciationPlan.MonthlyCharge;
                if (assetDepreciationPlan.TotalCumulativelyCharge + depreciation >= asset.InitialValue)
                {
                    next         = false;
                    depreciation = asset.InitialValue - assetDepreciationPlan.TotalCumulativelyCharge;
                }

                assetDepreciationPlan.TotalCurrentCharge      += depreciation;
                assetDepreciationPlan.TotalCumulativelyCharge += depreciation;
                assetDepreciationPlan.TotalRemainingAmount    -= depreciation;

                if (!years.ContainsKey(fistYear))
                {
                    yearIndex++;
                    years.Add(fistYear, fistYear);
                    AssetDepreciationYearPlan  assetDepreciationYearPlan  = new AssetDepreciationYearPlan();
                    AssetDepreciationMonthPlan assetDepreciationMonthPlan = new AssetDepreciationMonthPlan();

                    assetDepreciationMonthPlan.No                 = no;
                    assetDepreciationMonthPlan.MonthYear          = assetDataSet.MonthNames[fistMonth] + " " + fistYear.ToString();
                    assetDepreciationMonthPlan.CurrentCharge      = depreciation;
                    assetDepreciationMonthPlan.CumulativelyCharge = assetDepreciationPlan.TotalCumulativelyCharge;
                    assetDepreciationMonthPlan.RemainingAmount    = assetDepreciationPlan.TotalRemainingAmount;

                    assetDepreciationYearPlan.TotalYearCharge += depreciation;
                    assetDepreciationYearPlan.Year             = fistYear;
                    assetDepreciationYearPlan.AssetDepreciationMonthPlans.Add(assetDepreciationMonthPlan);

                    assetDepreciationPlan.AssetDepreciationYearPlans.Add(assetDepreciationYearPlan);
                }
                else
                {
                    AssetDepreciationYearPlan  assetDepreciationYearPlan  = assetDepreciationPlan.AssetDepreciationYearPlans[yearIndex];
                    AssetDepreciationMonthPlan assetDepreciationMonthPlan = new AssetDepreciationMonthPlan();

                    assetDepreciationMonthPlan.No                 = no;
                    assetDepreciationMonthPlan.MonthYear          = assetDataSet.MonthNames[fistMonth] + " " + fistYear.ToString();
                    assetDepreciationMonthPlan.CurrentCharge      = depreciation;
                    assetDepreciationMonthPlan.CumulativelyCharge = assetDepreciationPlan.TotalCumulativelyCharge;
                    assetDepreciationMonthPlan.RemainingAmount    = assetDepreciationPlan.TotalRemainingAmount;

                    assetDepreciationYearPlan.TotalYearCharge += depreciation;

                    assetDepreciationYearPlan.AssetDepreciationMonthPlans.Add(assetDepreciationMonthPlan);
                }
            }

            return(assetDepreciationPlan);
        }
Ejemplo n.º 3
0
        public void TestCalculateAssetDepreciationPlan()
        {
            Asset asset = new Asset()
            {
                Id = 2, AssetName = "Serwer HP", StartUsingDate = new DateTime(2017, 12, 8), InitialValue = (decimal)1000.21, AmortisedValue = 0, DepreciationTypeId = 1
            };
            Dictionary <int, string>           monthNames        = MocForMonths();
            Dictionary <int, DepreciationType> depreciationTypes = new Dictionary <int, DepreciationType>();

            depreciationTypes.Add(1, new DepreciationType()
            {
                Id = 1, Name = "Liniowa 30%", DepreciationRate = (decimal)30
            });
            List <DepreciationCharge> depreciationCharges = new List <DepreciationCharge>();

            depreciationCharges.Add(new DepreciationCharge()
            {
                Id = 10, No = 1, Month = 1, Year = 2018, CurrentCharge = (decimal)25.01, CumulativelyCharge = (decimal)25.01, RemainingAmount = (decimal)975.20, AssetId = 2
            });
            depreciationCharges.Add(new DepreciationCharge()
            {
                Id = 11, No = 2, Month = 2, Year = 2018, CurrentCharge = (decimal)25.01, CumulativelyCharge = (decimal)50.02, RemainingAmount = (decimal)950.19, AssetId = 2
            });
            depreciationCharges.Add(new DepreciationCharge()
            {
                Id = 12, No = 3, Month = 3, Year = 2018, CurrentCharge = (decimal)25.01, CumulativelyCharge = (decimal)75.03, RemainingAmount = (decimal)925.18, AssetId = 2
            });
            depreciationCharges.Add(new DepreciationCharge()
            {
                Id = 13, No = 4, Month = 4, Year = 2018, CurrentCharge = (decimal)25.01, CumulativelyCharge = (decimal)100.04, RemainingAmount = (decimal)900.17, AssetId = 2
            });
            depreciationCharges.Add(new DepreciationCharge()
            {
                Id = 14, No = 5, Month = 5, Year = 2018, CurrentCharge = (decimal)25.01, CumulativelyCharge = (decimal)125.05, RemainingAmount = (decimal)875.16, AssetId = 2
            });
            depreciationCharges.Add(new DepreciationCharge()
            {
                Id = 15, No = 6, Month = 6, Year = 2018, CurrentCharge = (decimal)25.01, CumulativelyCharge = (decimal)150.06, RemainingAmount = (decimal)850.15, AssetId = 2
            });
            depreciationCharges.Add(new DepreciationCharge()
            {
                Id = 16, No = 7, Month = 7, Year = 2018, CurrentCharge = (decimal)25.01, CumulativelyCharge = (decimal)175.07, RemainingAmount = (decimal)825.14, AssetId = 2
            });
            depreciationCharges.Add(new DepreciationCharge()
            {
                Id = 17, No = 8, Month = 8, Year = 2018, CurrentCharge = (decimal)25.01, CumulativelyCharge = (decimal)200.08, RemainingAmount = (decimal)800.13, AssetId = 2
            });
            depreciationCharges.Add(new DepreciationCharge()
            {
                Id = 18, No = 9, Month = 9, Year = 2018, CurrentCharge = (decimal)25.01, CumulativelyCharge = (decimal)225.09, RemainingAmount = (decimal)775.12, AssetId = 2
            });
            depreciationCharges.Add(new DepreciationCharge()
            {
                Id = 19, No = 10, Month = 10, Year = 2018, CurrentCharge = (decimal)25.01, CumulativelyCharge = (decimal)250.10, RemainingAmount = (decimal)750.11, AssetId = 2
            });
            depreciationCharges.Add(new DepreciationCharge()
            {
                Id = 20, No = 11, Month = 11, Year = 2018, CurrentCharge = (decimal)25.01, CumulativelyCharge = (decimal)275.11, RemainingAmount = (decimal)725.10, AssetId = 2
            });
            depreciationCharges.Add(new DepreciationCharge()
            {
                Id = 21, No = 12, Month = 12, Year = 2018, CurrentCharge = (decimal)25.01, CumulativelyCharge = (decimal)300.12, RemainingAmount = (decimal)700.09, AssetId = 2
            });
            depreciationCharges.Add(new DepreciationCharge()
            {
                Id = 22, No = 13, Month = 1, Year = 2019, CurrentCharge = (decimal)25.01, CumulativelyCharge = (decimal)325.13, RemainingAmount = (decimal)675.08, AssetId = 2
            });
            depreciationCharges.Add(new DepreciationCharge()
            {
                Id = 23, No = 14, Month = 2, Year = 2019, CurrentCharge = (decimal)25.01, CumulativelyCharge = (decimal)350.14, RemainingAmount = (decimal)650.07, AssetId = 2
            });
            depreciationCharges.Add(new DepreciationCharge()
            {
                Id = 24, No = 15, Month = 3, Year = 2019, CurrentCharge = (decimal)25.01, CumulativelyCharge = (decimal)375.15, RemainingAmount = (decimal)625.06, AssetId = 2
            });

            AssetDataSet assetDataSet = new AssetDataSet()
            {
                MonthNames = monthNames, DepreciationTypes = depreciationTypes, DepreciationCharges = depreciationCharges
            };

            Depreciation          depreciation          = new Depreciation();
            AssetDepreciationPlan assetDepreciationPlan = depreciation.CalculateAssetDepreciationPlan(assetDataSet, asset, true);

            decimal total11 = 0;
            decimal total12 = 0;
            decimal total13 = 0;
            decimal total21 = 0;
            decimal total31 = assetDepreciationPlan.TotalCurrentCharge;
            decimal total32 = assetDepreciationPlan.TotalCumulativelyCharge;
            decimal total33 = assetDepreciationPlan.TotalRemainingAmount;

            foreach (AssetDepreciationYearPlan assetDepreciationYearPlan in assetDepreciationPlan.AssetDepreciationYearPlans)
            {
                foreach (AssetDepreciationMonthPlan assetDepreciationMonthPlan in assetDepreciationYearPlan.AssetDepreciationMonthPlans)
                {
                    total11 += assetDepreciationMonthPlan.CurrentCharge;
                    total12  = assetDepreciationMonthPlan.CumulativelyCharge;
                    total13  = assetDepreciationMonthPlan.RemainingAmount;
                }
                total21 += assetDepreciationYearPlan.TotalYearCharge;
            }

            Assert.AreEqual(total11, total21);
            Assert.AreEqual(total31, total21);

            Assert.AreEqual(total12, total32);
            Assert.AreEqual(total13, total33);
        }