yanweiyuan3
2023-08-09 588bc7829387dfc761cc25f06f77d4c81818bd10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Quintiq file version 2.0
#parent: #root
Method SetPeriodWithNrOfOccurrences (
  Date startDate_i,
  Number nrOfOccurrences_i
)
{
  Description: 'Set a recurrence period that starts at a specific date recurs for a specific number of times.'
  TextBody:
  [*
    // Check if a new RecurrencePeriod must be created.
    period       := guard( this.RecurrencePeriod().astype( LibCal_RecurrencePeriodNrOfOccurrences ), null( LibCal_RecurrencePeriodNrOfOccurrences ) );
    changeOfType := isnull( period );
    
    isChanged := changeOfType  // There was no period yet, or it was not a PeriodNrOfOccurrences
              or period.StartDate()       <> startDate_i
              or period.NrOfOccurrences() <> nrOfOccurrences_i;
    
    if( isChanged )
    {
      if( nrOfOccurrences_i >= 0 )
      {
        prevPeriod    := this.GetRecurrencePeriod(); // Can be the one from the LeadingParticipation.
        prevStartDate := guard( prevPeriod.StartDate(), Date::MinDate() );
        prevEndDate   := guard( prevPeriod.EndDate(),   Date::MaxDate() );
      
        if( changeOfType )
        {
          // Create a new RecurrencePeriod.
          period := LibCal_RecurrencePeriodNrOfOccurrences::Create( this, startDate_i, nrOfOccurrences_i );
        }
        else
        {
          period.StartDate( startDate_i );
          period.NrOfOccurrences( nrOfOccurrences_i );
        }
        
        // Directly calculate the new EndDate.
        period.CalcEndDate();
      
        // Register the previous startdate and enddate.
        // They are used to determine which occurrences must be created / deleted for the changed period.
        period.PreviousStartDate( prevStartDate );
        period.PreviousEndDate(   prevEndDate   );
      
        this.HasChangedRecurrencePeriod( true );
      }
      else
      {
        LibCal_Util::Error( this.Calendar().CalendarID() + "." + this.Event().Subject() + " : " + 
                            Translations::Period_InvalidNrOfOccurrences() + "(is " + [String]nrOfOccurrences_i + ")" );
      }    
    }
  *]
  InterfaceProperties { Accessibility: 'Module' }
}