2012-12-12 144 views
0

我正在尋找一些幫助來計算新調度系統的到期日期。C#計算日期

目前,日程安排是基於每月或每週付款,從定義的日期開始。

在新的時間表中,我們希望這是基於客戶支付頻率,從定義日期開始。

我們有薪頻率13個選項:

  1. 每月的 - 最後一個工作日
  2. 月 - 同日例如20日,25日
  3. 月 - 1日週一/週二/週三/週四/週五
  4. 月 - 2週一/週二/週三/週四/週五
  5. 月 - 3日週一/週二/週三/週四/週五
  6. 月 - 最後週一/週二/週三/週四/週五
  7. 週刊 - 週一
  8. 週刊 - 週二
  9. 週刊 - 週三
  10. 週刊 - 週四
  11. 週刊 - 週五
  12. 4周
  13. 雙週

根據傳入的薪酬頻率和支付,尾款的數量,我需要產生一個新的時間表。

第一次付款是直截了當的,因爲它是在傳遞的日期,日程表中的其他日期需要從該日期計算(取決於工資頻率)。

我還需要確保預定的日期在週一至週五,並且不在公共/銀行假期登陸 - 在這種情況下,我將恢復到下一個有效的一週。

我到目前爲止的方法如下 - 與區域我需要幫助評論:

//計算一個繳費日期

public IList<ScheduledInstalment> GenerateSchedule(int agreementID, int paymentCount, 
    PayFrequency frequency, double balance, DateTime firstPaymentDate) 
{ 
    IList<ScheduledInstalment> schedule = new List<ScheduledInstalment>(); 

    PaymentCalculation calc = GetPaymentCalculation(frequency, firstPaymentDate); 

    double regularInstalment = Math.Round(balance/paymentCount, 1); 
    double finalInstalment = Math.Round(((regularInstalment * (paymentCount - 1)) - balance), 2); 

    for (int i = 0; i <= paymentCount; i++) 
    { 
     ScheduledInstalment s = new ScheduledInstalment(); 

     s.AgreementID = agreementID; 

     if (i == 0) 
     { 
      // First Payment 
      s.DueDate = firstPaymentDate; 
      s.AmountDue = regularInstalment; 
     } 
     else 

      // Calculate next payment date 

      if (i < paymentCount) 
      { 
       // Regular Payment 
       s.AmountDue = regularInstalment; 
      } 
      else 
      { 
       // Final Payment 
       s.AmountDue = finalInstalment; 
      } 

     schedule.Add(s); 
    } 

    return schedule; 
} 
+6

你的問題是什麼?...或,有什麼問題? –

+0

我認爲他需要'else'和'if if(i MikeTheLiar

+0

這是對的Mike,我需要幫助確定每個循環中的截止日期。 –

回答

1

OK,我設法得到這似乎工作,這裏是我的方法如下:

public DateTime GetNextRepaymentDate(DateTime BaseDate, int instalmentCount, PaymentCalculation calc) 
      { 
       DateTime dueDate = new DateTime(); 

       switch (calc.Interval) 
       { 
        case DateInterval.Month: 

         dueDate = BaseDate.AddMonths((instalmentCount) * calc.Number); 

         if (!string.IsNullOrEmpty(calc.OtherCriteria)) 
         { 
          if (calc.OtherCriteria == "Last") 
          { 
           int lastDay = DateTime.DaysInMonth(dueDate.Year, dueDate.Month); 
           dueDate = Convert.ToDateTime(string.Format("{0}/{1}/{2}", lastDay, dueDate.Month, dueDate.Year)); 
          } 
          else 
          { 
           int fixedDate = Convert.ToInt32(calc.OtherCriteria); 

           if (dueDate.Day != fixedDate) 
           { 
            if (dueDate.Day > fixedDate) 
            { 
             while (dueDate.Day != fixedDate) 
             { 
              dueDate = dueDate.AddDays(-1); 
             } 
            } 
            else 
            { 
             while (dueDate.Day != fixedDate) 
             { 
              dueDate = dueDate.AddDays(1); 
             } 
            } 
           } 
          } 
         } 

         break; 

        case DateInterval.WeekOfYear: 

         dueDate = BaseDate.AddDays((instalmentCount) * (calc.Number * 7)); 

         if (calc.FixedDay != null) 
         { 
          while (dueDate.DayOfWeek != calc.FixedDay) 
          { 
           dueDate = dueDate.AddDays(-1); 
          } 
         } 

         break; 
       } 

       while (!PaymentIsAllowedOnDate(dueDate) == true) 
       { 
        dueDate = dueDate.AddDays(-1); 
       } 

       return dueDate; 
      }