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
57
58
Quintiq file version 2.0
#parent: #root
StaticMethod GetNumberOfPeriodsForDifferentTimeUnits (
  String smallertimeunit,
  String biggertimeunit,
  DateTime periodstartdate
) const declarative remote as Real
{
  Description:
  [*
    Return the number of period of the larger time unit when converting to a smaller time unit. For example, a month convert to weeks = 4 weeks.
    This doesn't care the argument periodstartdate is at the beginning or middle or end of the month. For example, a month convert to weeks, with periodstartdate =  15 Jan, still returning 4 weeks.
  *]
  TextBody:
  [*
    // DWE2 Apr-25-2016 (modified)
    
    numberofperiod := 0.0;
    
    // Always take the "bigger" time unit to convert to the "smaller" time unit to avoid decimal point.
    starttime := periodstartdate;
    start := starttime.Date();
    
    // Find the end date of the "bigger" time unit. E.g., 1-Jan-2012 for time unit Month will return 1-Feb-2012.
    endtime := PeriodSpecification_MP::GetStartOfNextPeriod( starttime, biggertimeunit, 1 );
    end := endtime.Date();
    
    //Get number of periods between start and end of the smaller time unit
    if( smallertimeunit = Translations::MP_GlobalParameters_Hour() )
    { 
      numberofperiod := ( endtime - starttime ).HoursAsReal();
    }
    else if( smallertimeunit = Translations::MP_GlobalParameters_Day() )
    {
      numberofperiod := end - start;
    }
    else if( smallertimeunit = Translations::MP_GlobalParameters_Week() )
    {
      numberofperiod := ( end - start ) / 7;
    }
    else if( smallertimeunit = Translations::MP_GlobalParameters_Month()
             or smallertimeunit = Translations::MP_GlobalParameters_Quarter() )
    {
      numberofperiod := ( ( end.Year() - start.Year() ) * 12 ) +  end.Month() - start.Month();
    
      if( smallertimeunit = Translations::MP_GlobalParameters_Quarter() )
      {
        numberofperiod := numberofperiod / 3;
      }
    }
    else if( smallertimeunit = Translations::MP_GlobalParameters_Year() )
    {
      numberofperiod := end.Year() - start.Year();
    }
    
    return numberofperiod;
  *]
}