none
How to show progressive calculation? RRS feed

  • Question

  • I am developing a web application in asp.Net MVC.

    I have the following tables:

    1. Employee(Id, Name, Personal No)

    2. Recovery(Id, EmpId, Month, Year, Amount)

    Let the data is as under:

    Employee:

    1             John          5252

    2              Peter         4847

    Recovery:

    1           2           Feb      2018       20000

    From this, I need to generate a view as under:

    Recovery Statement of Peter

    Month             Year           Recovery            Balance

    Feb                 2018           5000                 15000

    Mar                 2018           5000                 10000

    April                2018           5000                  5000

    May                 2018           5000                  0

    You may assume any fixed amount to be recovered every month.

    Regards,

    Arun

    Sunday, January 7, 2018 4:24 PM

Answers

  • Hi ArunKhatri,

    >>You may assume any fixed amount to be recovered every month.

    According to your description, if the value of Recovery is fixed, you could implement it via c# like this:

    1. Define a class and an enum like this:

    public class RecoveryDto
        {
            public string Month { get; set; }
    
            public int Year { get; set; }
    
            public int Recovery { get; set; }
    
            public int Balance { get; set; }
        }
    
        public enum Months
        {
            Jan = 1,
            Feb = 2,
            Mar = 3,
            April = 4,
            May = 5,
            June = 6,
            July = 7,
            Aug = 8,
            Sep = 9,
            Oct = 10,
            Nov = 11,
            Dec = 12
        }

    2. related c# code.

    int recovery = 5000;
                int count = 0;
                List<RecoveryDto> list = new List<RecoveryDto>();
                Months baseMonths = Months.Jan;
                int baseYear = 2018;
                DateTime baseDate = DateTime.Now;
                DateTime oldDate = DateTime.Now;
                using (var db = new EFDemoContext())
                {
                    int? amount = db.Recoveries.Where(t=>t.Id == 1).Select(t=>t.Amount).FirstOrDefault();
                    string month = db.Recoveries.Where(t => t.Id == 1).Select(t => t.Month).FirstOrDefault();
                    int year = db.Recoveries.Where(t => t.Id == 1).Select(t => t.Year).FirstOrDefault();
                    int baseBalance = 0;
                    if (amount.HasValue && amount > 0)
                    {
                        count = (amount.Value - 1) / recovery + 1;
                        for (int i = 0; i < count; i++)
                        {
                            RecoveryDto rd = new RecoveryDto();
                            if (i == 0)
                            {
                                baseMonths = (Months)Enum.Parse(typeof(Months), month);
                                baseYear = year;
                                baseDate = new DateTime(year, (int)baseMonths, 1);
                                oldDate = new DateTime(year, (int)baseMonths, 1);
                                baseBalance = amount.Value - recovery;
                            }
                            else
                            {
                                baseDate = oldDate.AddMonths(1);
                                baseMonths = (Months)baseDate.Month;
                                baseYear = baseDate.Year;
                                baseBalance = baseBalance - recovery;
                            }
                            
                            rd.Year = baseYear;
                            rd.Month = baseMonths.ToString();
                            rd.Recovery = recovery;
                            rd.Balance = baseBalance;
    
                            list.Add(rd);
                            oldDate = baseDate;
                        }
    
                   }
                }

     Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by ArunKhatri Tuesday, January 9, 2018 12:13 PM
    Monday, January 8, 2018 2:13 AM
    Moderator