﻿// suggest periodic distribution
// ----------------------------- 


Parameter MODE   // D,W,M,Q,Y,RESET

if U[MODE=RESET]
  Clear V[me21ndis]
  Clear V[me21ndis.stat.*]
  Return 
endif



Set V[date] "&V[me21n_eeind]"
Set V[quantity] "&V[me21n_menge]"

if not V[me21n_distribute_each>0]
  Set V[me21n_distribute_each] 1
endif

Set V[k] 1
Clear V[me21ndis]

label next_day


if V[quantity>0]

   
  // remaining quantity
  if V[quantity<&V[me21n_distribute_each]]
    Set V[me21ndis.quantity.&V[k]]  "&V[quantity]"
    Set V[quantity] 0
  else 
    Set V[me21ndis.quantity.&V[k]]  "&V[me21n_distribute_each]"
    Set V[quantity] &V[quantity] - "&V[me21n_distribute_each]"
  endif
  
  
  // next working day
  Set V[factory_calendar] "01"
  Call "DATE_CONVERT_TO_FACTORYDATE"  _
    export.DATE="date" _
    export.FACTORY_CALENDAR_ID="factory_calendar" _
    import.DATE="date2"
  
  // set date
  Set V[me21ndis.date.&V[k]]  "&V[date2]"
  
 
  
  // days
  if U[MODE=D]
    Set V[date] &V[date2] + 1
  endif
  
  // weeks
  if U[MODE=W]
    Set V[date] &V[date] + 7
  endif
  
  // months
  if U[MODE=M] or  U[MODE=Q] or U[MODE=Y]
    
    Set V[months] 1
    if U[MODE=Q] 
      Set V[months] 3  // quarter
    endif
    if U[MODE=Y] 
      Set V[months] 12 // year
    endif
    
    // add months
    Call "RE_ADD_MONTH_TO_DATE" _
      export.OLDDATE="date" _
      export.MONTHS="months" _
      import.NEWDATE="date"
  endif
  
  
  
  // next one
  Set V[k] &V[k] + 1
  
  
  goto next_day
  
endif