Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Mar 1st, 2012  |  syntax: Scala  |  size: 2.11 KB  |  hits: 56  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package com.linxberg.ta.core.business.rules
  2.  
  3. import com.linxberg.ta.core.business.Context
  4. import com.linxberg.ta.core.business.TimeInterval
  5. import com.linxberg.ta.core.entities.PayPolicy
  6. import com.linxberg.ta.core.business.TimeInterval
  7. import com.linxberg.ta.core.business.TimeInterval._
  8. import com.linxberg.ta.core.util.Grapher
  9. import org.scala_tools.time.Imports._
  10. import org.joda.time.Duration
  11.  
  12. object DailyOT extends Rule {
  13.  
  14.   override val fnDesc: String = "Daily overtime"
  15.   override val activityCode: String = "OT"
  16.  
  17.   override def applyRuleTo(ctx: Context) = {
  18.     val pp = ctx("PP"): PayPolicy
  19.     val days = ctx("BD"): List[TimeInterval]
  20.  
  21.     def calc(list: List[TimeInterval], code:String, hoursThreshold:Int) =
  22.       if (hoursThreshold > 0)
  23.               list.zip(list.scanLeft(Duration.ZERO)(_ + _.interval.duration)).collect {
  24.                 case (ti, tot) if tot > hoursThreshold.hours => ti.withCode(code)
  25.                 case (ti, tot) if tot + ti.interval.duration > hoursThreshold.hours => {
  26.                    val diffPtv =  (tot + ti.interval.duration) - hoursThreshold.hours
  27.                    TimeInterval(code, ti.end - diffPtv, ti.end)
  28.                 }  
  29.             }
  30.       else
  31.         Nil
  32.    
  33.     def mergeVectors(v1:List[TimeInterval], v2:List[TimeInterval]) = {
  34.       val (part1, part2) = v1.partition(ot=> !v2.exists(_? ot))
  35.       val intersect = if (part2.nonEmpty && v2.nonEmpty) {
  36.         val (first,second) = (part2.head, v2.head)
  37.         if (!(first ≡ second))
  38.                 Seq(TimeInterval(first.code, first.start, (first ∩ second).start))
  39.         else
  40.                 Nil
  41.       }
  42.       else Nil
  43.      
  44.       part1 ++ intersect ++ v2
  45.      
  46.     }
  47.    
  48.    
  49.     val state = if (pp.otDayHours > 0)
  50.       days.foldLeft(Nil: List[TimeInterval]) { (result, e) =>  
  51.         val allProds = e.children.flatMap(_.children)
  52.         result ++ Seq(calc(allProds, "OT1", pp.otDayHours),
  53.             calc(allProds, "OT2", pp.ot2DayHours),
  54.             calc(allProds, "OT3", pp.ot3DayHours)).reduceLeft(mergeVectors)
  55.     }
  56.     else
  57.         Nil
  58.            
  59.     ctx + Map("OT1"->Nil, "OT2"->Nil, "OT3"->Nil) + state.groupBy(_.code)
  60.   }
  61.  
  62.  
  63. }