#!/bin/sh  
# \
exec oagwish "$0" "$@"

set auto_path [linsert $auto_path 0  /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0 /usr/local/oag/lib_patch/$env(HOST_ARCH)]

set CVSRevisionAuthor "\$Revision: 1.1 $ \$Author: soliday $"

APSApplication . -name CheckPayStub -version $CVSRevisionAuthor

pack [frame .userFrame.top]
pack [frame .userFrame.top.left] -side left -anchor nw
pack [frame .userFrame.top.start] -side left -anchor nw
pack [frame .userFrame.top.stop] -side left -anchor nw
pack [frame .userFrame.top.tech] -side left -anchor nw
pack [frame .userFrame.top.lunch] -side left -anchor nw
pack [frame .userFrame.top.afternoon] -side left -anchor nw
pack [frame .userFrame.top.midnight] -side left -anchor nw
pack [frame .userFrame.top.doubleback] -side left -anchor nw
pack [frame .userFrame.top.vacation] -side left -anchor nw

set startShift 0
APSRadioButtonFrame .selectShift \
  -parent .userFrame.top.left \
  -label "Select shift to start from:" \
  -variable startShift \
  -buttonList {"Day Shift" \
                 "Afternoon Shift" "Afternoon/Night Shift" \
                 "Night Shift" "Backup Shift"} \
  -valueList "0 1 2 3 4" \
  -commandList "ListEachDay ListEachDay ListEachDay ListEachDay ListEachDay" \
  -packOption "-side top -fill x -anchor nw"

set aco 0
APSRadioButtonFrame .selectACO \
  -parent .userFrame.top.left \
  -label "" \
  -variable aco \
  -buttonList {"ACO" "Operator"} \
  -valueList "1 0" \
  -commandList "ListEachDay ListEachDay" \
  -packOption "-side top -fill x -anchor nw"


set startDay 0

set numOfDays 14

set hourlyPay 0.0
APSLabeledEntry .hourlyPay \
  -parent .userFrame.top.left \
  -label "Hourly Pay:" \
  -type real \
  -textVariable hourlyPay \
  -width 10 \
  -packOption "-side top -fill x -anchor nw"

APSButton .calcPay \
  -parent .userFrame \
  -text "Calculate Pay" \
  -command CalculatePay

APSButton .details \
  -parent .userFrame \
  -text "Show Details" \
  -command ShowDetails

APSLabeledOutput .pay \
  -parent .userFrame \
  -label "Gross Pay:" \
  -textVariable grossPay \
  -packOption "-anchor ne"

set w .userFrame.top.start
set w2 .userFrame.top.stop
set w3 .userFrame.top.tech
set w4 .userFrame.top.lunch
set w5 .userFrame.top.afternoon
set w6 .userFrame.top.midnight
set w7 .userFrame.top.doubleback
set w8 .userFrame.top.vacation
set dayNames "Monday Tuesday Wednesday Thursday Friday Saturday Sunday"
pack [label $w.label -text "           Start"]
pack [label $w2.label -text "    Stop"]
pack [label $w3.label -text "Lead Tech(+$.95)"]
pack [label $w4.label -text "   Lunch   "]
pack [label $w5.label -text "Afternoon(+$.85)"]
pack [label $w6.label -text "  Midnight(+$.90)   "]
pack [label $w7.label -text "Double Back"]
pack [label $w8.label -text "  Vacation  "]
for {set i 0} {$i < $numOfDays} {incr i} {
    set day [expr ($startShift * 7 + $i + $startDay)%35]
    APSLabeledEntry .day$i \
      -parent $w \
      -label "  [lindex $dayNames [expr ($i + $startDay)%7]]" \
      -textVariable start(day$i)
    if {[expr $i%2]} {
        $w.day$i configure -bg grey
        $w.day$i.label configure -bg grey
    }
    
    APSLabeledEntry .day$i \
      -parent $w2 \
      -label " - " \
      -textVariable end(day$i)
    if {[expr $i%2]} {
        $w2.day$i configure -bg grey
        $w2.day$i.label configure -bg grey
    }

    set tech(day$i) 0
    checkbutton $w3.day$i \
      -variable tech(day$i)
    pack $w3.day$i -pady 1 -fill x
    if {[expr $i%2]} {
        $w3.day$i configure -bg grey
    }

    set lunch(day$i) 0
    checkbutton $w4.day$i \
      -variable lunch(day$i)
    pack $w4.day$i -pady 1 -fill x
    if {[expr $i%2]} {
        $w4.day$i configure -bg grey
    }

    set afternoon(day$i) 0
    checkbutton $w5.day$i \
      -variable afternoon(day$i)
    pack $w5.day$i -pady 1 -fill x
    if {[expr $i%2]} {
        $w5.day$i configure -bg grey
    }

    set midnight(day$i) 0
    checkbutton $w6.day$i \
      -variable midnight(day$i)
    pack $w6.day$i -pady 1 -fill x
    if {[expr $i%2]} {
        $w6.day$i configure -bg grey
    }

    set double(day$i) 0
    checkbutton $w7.day$i \
      -variable double(day$i)
    pack $w7.day$i -pady 1 -fill x
    if {[expr $i%2]} {
        $w7.day$i configure -bg grey
    }

    set vacation(day$i) 0
    checkbutton $w8.day$i \
      -variable vacation(day$i) \
      -command VacationChecked
    pack $w8.day$i -pady 1 -fill x
    if {[expr $i%2]} {
        $w8.day$i configure -bg grey
    }
}

proc VacationChecked {args} {
    global vacation start end numOfDays double midnight afternoon lunch tech
    for {set i 0} {$i < $numOfDays} {incr i} {
        if {$vacation(day$i)} {
            set start(day$i) "VACATION"
            set end(day$i) "8:00 hours"
            set double(day$i) 0
            set midnight(day$i) 0
            set afternoon(day$i) 0
            set lunch(day$i) 0
            set tech(day$i) 0
            set double(day[expr {$i + 1}]) 0
        } else {
            if {$start(day$i) == "VACATION"} {
                set start(day$i) ""
                set end(day$i) ""
            }
        }
    }
}

proc SetDefaultSchedule {args } {
    global defaultStart defaultEnd aco
    set defaultStart(day0) 7:00
    set defaultEnd(day0) 15:10
    set defaultStart(day1) 7:00
    set defaultEnd(day1) 15:10
    set defaultStart(day2) 7:00
    set defaultEnd(day2) 15:10
    set defaultStart(day3) 7:00
    set defaultEnd(day3) 15:10
    set defaultStart(day4) 7:00
    set defaultEnd(day4) 15:10
    set defaultStart(day5) ""
    set defaultEnd(day5) ""
    set defaultStart(day6) ""
    set defaultEnd(day6) ""
    
    set defaultStart(day7)  ""
    set defaultEnd(day7)  ""
    set defaultStart(day8)  ""
    set defaultEnd(day8)  ""
    set defaultStart(day9) 7:00
    set defaultEnd(day9) 15:30
    set defaultStart(day10) 15:00
    set defaultEnd(day10) 23:10
    set defaultStart(day11) 15:00
    set defaultEnd(day11) 23:10
    if {$aco} {
        set defaultStart(day12) 10:45
        set defaultEnd(day12) 23:10
        set defaultStart(day13) 10:45
        set defaultEnd(day13) 23:10
    } else {
        set defaultStart(day12) 11:00
        set defaultEnd(day12) 23:10
        set defaultStart(day13) 11:00
        set defaultEnd(day13) 23:10
    }

    set defaultStart(day14) 15:00
    set defaultEnd(day14) 23:10
    set defaultStart(day15) 15:00
    set defaultEnd(day15) 23:10
    set defaultStart(day16) 15:00
    set defaultEnd(day16) 23:10
    set defaultStart(day17) ""
    set defaultEnd(day17) ""
    set defaultStart(day18) ""
    set defaultEnd(day18) ""
    set defaultStart(day19) -1:00
    set defaultEnd(day19) 11:10
    set defaultStart(day20) -1:00
    set defaultEnd(day20) 11:10
    
    set defaultStart(day21) -1:00
    set defaultEnd(day21) 7:10
    set defaultStart(day22) -1:00
    set defaultEnd(day22) 7:10
    set defaultStart(day23) -1:00
    set defaultEnd(day23) 7:10
    set defaultStart(day24) -1:00
    set defaultEnd(day24) 7:10
    set defaultStart(day25) -1:00
    set defaultEnd(day25) 7:10
    set defaultStart(day26) ""
    set defaultEnd(day26) ""
    set defaultStart(day27) ""
    set defaultEnd(day27) ""
    
    set defaultStart(day28) 7:00
    set defaultEnd(day28) 15:30
    set defaultStart(day29) 7:00
    set defaultEnd(day29) 15:30
    set defaultStart(day30) 7:00
    set defaultEnd(day30) 15:30
    set defaultStart(day31) 7:00
    set defaultEnd(day31) 15:30
    set defaultStart(day32) 7:00
    set defaultEnd(day32) 15:30
    set defaultStart(day33) ""
    set defaultEnd(day33) ""
    set defaultStart(day34) ""
    set defaultEnd(day34) ""
}




proc ListEachDay {args} {
    unset -nocomplain start end tech lunch midnight afternoon
    global startShift startDay numOfDays defaultStart defaultEnd grossPay
    global start end tech lunch midnight afternoon double vacation
    SetDefaultSchedule
    for {set i 0} {$i < $numOfDays} {incr i} {
        set day [expr ($startShift * 7 + $i + $startDay)%35]
        set start(day$i) [set defaultStart(day${day})]
        set end(day$i) [set defaultEnd(day${day})]
        set tech(day$i) 0
        if {$end(day$i) == "15:30"} {
            set lunch(day$i) 1
        } else {
            set lunch(day$i) 0
        }
        if {$start(day$i) == "-1:00"} {
            set midnight(day$i) 1
        } else {
            set midnight(day$i) 0
        }
        if {$day == 13} {
            set midnight(day$i) 1
            set afternoon(day$i) 0
        } elseif {$start(day$i) == "15:00"} {
            set afternoon(day$i) 1
        } elseif {($start(day$i) == "11:00") && ($end(day$i) == "23:10")} {
            set afternoon(day$i) 1
        } elseif {($start(day$i) == "10:45") && ($end(day$i) == "23:10")} {
            set afternoon(day$i) 1
        } else {
            set afternoon(day$i) 0
        }
        if {$day == 12} {
            set double(day$i) 1
        } else {
            set double(day$i) 0
        }
        set vacation(day$i) 0
    }
    set grossPay ""
}

proc CalculatePay {args} {
    set details 0
    APSStrictParseArguments {details}
    global startShift startDay defaultStart defaultEnd aco vacation numOfDays
    global start end grossPay hourlyPay tech lunch midnight afternoon double
    set dayNames "Monday Tuesday Wednesday Thursday Friday Saturday Sunday"


    set doubleBackMinutes 0
    set pst ""
    if {($double(day0) == 1)} {
        global MondayDoubleBack sundaystart
        APSDialogBox .question \
          -name "Double Back Question" \
          -okCommand "set MondayDoubleBack 1" \
          -cancelCommand "set MondayDoubleBack 0"
        set sundaystart 12:00
        APSLabeledEntry .sunday \
          -parent .question.userFrame \
          -label "Previous Sunday Start Time:" \
          -textVariable sundaystart \
          -width 10
        tkwait variable MondayDoubleBack
        if {$MondayDoubleBack} {
            set tmp [split $sundaystart :]
            if {[llength $tmp] != 2} {
                set pst ""
                continue
            }
            set startHour [lindex $tmp 0]
            set startMinute [lindex $tmp 1]
            set pst [expr {$startHour + ($startMinute / 60.0)}]
        } else {
            set double(day0) 0
        }
    }
    for {set i 0} {$i < $numOfDays} {incr i} {
        set tmp [split $start(day$i) :]
        if {[llength $tmp] != 2} {
            set pst ""
            continue
        }
        set startHour [string trimleft [lindex $tmp 0] 0]
        if {![llength $startHour]} {
            set startHour 0
        }
        set startMinute [string trimleft [lindex $tmp 1] 0]
        if {![llength $startMinute]} {
            set startMinute 0
        }

        set tmp [split $end(day$i) :]
        if {[llength $tmp] != 2} {
            set pst ""
            continue
        }
        set endHour [string trimleft [lindex $tmp 0] 0]
        if {![llength $endHour]} {
            set endHour 0
        }
        set endMinute [string trimleft [lindex $tmp 1] 0]
        if {![llength $endMinute]} {
            set endMinute 0
        }
        
        set st [expr {$startHour + ($startMinute / 60.0)}]
        if {[llength $pst]} {
            if {$st < $pst} {
                set min [expr {($pst - $st) * 60.0}]
                if {$i < 7} {
                    set part "the first"
                } else {
                    set part "the second"
                }
                if {$aco} {
                    set num 240
                } else {
                    set num 239.9
                }
                if {$min <= $num} {
                    set answer [tk_dialog .question "Double Back Question" "Was your schedule for $part [lindex $dayNames [expr $i%7]] changed by the lab without a request from you?" question 0 Yes No]
                    if {$answer == 0} {
                        set double(day$i) 1
                        set doubleBackMinutes [expr {$doubleBackMinutes + $min}]
                        set doubleMin(day$i) $min
                    } else {
                        set double(day$i) 0
                    }
                } else {
                    if {($double(day$i) == 0)} {
                        set answer [tk_dialog .question "Double Back Question" "Was your schedule for $part [lindex $dayNames [expr $i%7]] changed by the lab without a request from you?" question 0 Yes No]
                        if {$answer == 0} {
                            set double(day$i) 1
                            set doubleBackMinutes [expr {$doubleBackMinutes + $min}]
                            set doubleMin(day$i) $min
                        }
                    } else {
                        set doubleBackMinutes [expr {$doubleBackMinutes + $min}]
                        set doubleMin(day$i) $min
                    }
                }
            } else {
                set double(day$i) 0
            }
        }
        set pst $st
    }

    set output "Base Pay = \$[format %5.2f $hourlyPay] / hour\n\n"

    set grossPay 0
    for {set i 0} {$i < $numOfDays} {incr i} {
        set hp $hourlyPay
        
        append output "--------- [lindex $dayNames [expr $i%7]] ---------\n"

        if {$vacation(day$i)} {
            set tmp [split [lindex $end(day$i) 0] :]
            if {[llength $tmp] != 2} {
                continue
            }
            set vacationHour [string trimleft [lindex $tmp 0] 0]
            if {![llength $vacationHour]} {
                set vacationHour 0
            }
            set vacationMinute [string trimleft [lindex $tmp 1] 0]
            if {![llength $vacationMinute]} {
                set vacationMinute 0
            }
            set minutesWorkedToday [expr {($vacationHour * 60) + $vacationMinute}]
            set grossPay [expr {$grossPay + (($minutesWorkedToday / 60.0) * $hp)}]

            append output "$minutesWorkedToday minutes ([format %6.3f [expr {$minutesWorkedToday / 60.0}]] hours) of vacation\n"
            append output "(${minutesWorkedToday}minutes / 60minutes) * \$[format %5.2f $hp] = \$[expr {($minutesWorkedToday / 60.0) * $hp}]\n\n"
            
            continue
        }

        set tmp [split $start(day$i) :]
        if {[llength $tmp] != 2} {
            continue
        }
        set startHour [string trimleft [lindex $tmp 0] 0]
        if {![llength $startHour]} {
            set startHour 0
        }
        set startMinute [string trimleft [lindex $tmp 1] 0]
        if {![llength $startMinute]} {
            set startMinute 0
        }

        set tmp [split $end(day$i) :]
        if {[llength $tmp] != 2} {
            continue
        }
        set endHour [string trimleft [lindex $tmp 0] 0]
        if {![llength $endHour]} {
            set endHour 0
        }
        set endMinute [string trimleft [lindex $tmp 1] 0]
        if {![llength $endMinute]} {
            set endMinute 0
        }
        
        if $tech(day$i) {
            set hp [expr {$hp + .95}]
        }
        if $midnight(day$i) {
            set hp [expr {$hp + .90}]
        }
        if $afternoon(day$i) {
            set hp [expr {$hp + .85}]
        }
        
        set minutesWorkedToday [expr {($endHour - $startHour) * 60 - $startMinute + $endMinute}]
        if {$lunch(day$i)} {
            set minutesWorkedToday [expr {$minutesWorkedToday - 30}]
        }

        set overtimeMinutes 0
        if {$minutesWorkedToday >= 720} {
            set overtimeMinutes 240
        } elseif {$minutesWorkedToday > 480} {
            set overtimeMinutes [expr {$minutesWorkedToday - 480}]
        }
        if {$double(day$i)} {
            set overtimeMinutes [expr {$overtimeMinutes + $doubleMin(day$i)}]
            if {$overtimeMinutes > 240} {
                set overtimeMinutes 240
            }
        }

        set doubleTimeMinutes 0
        if {$minutesWorkedToday > 720} {
            set doubleTimeMinutes [expr {$minutesWorkedToday - 720 + 30}]
        }

        set grossPay [expr {$grossPay + (($minutesWorkedToday / 60.0) * $hp) + (($overtimeMinutes / 60.0) * ($hp * .5)) + (($doubleTimeMinutes / 60.0) * $hp)}]

        if {$tech(day$i)} {
            append output "Lead Tech pay of \$[format %5.2f $hp] / hour\n"
        }
        if {$afternoon(day$i)} {
            append output "Afternoon pay of \$[format %5.2f $hp] / hour\n"
        }
        if {$midnight(day$i)} {
            append output "Midnight pay of \$[format %5.2f $hp] / hour\n"
        }
        append output "[format %3d $minutesWorkedToday] minutes            ([format %6.3f [expr {$minutesWorkedToday / 60.0}]] hours) worked\n"
        append output "[format %3d $overtimeMinutes] overtime minutes   ([format %6.3f [expr {$overtimeMinutes / 60.0}]] overtime hours) worked\n"
        append output "[format %3d $doubleTimeMinutes] doubletime minutes ([format %6.3f [expr {$doubleTimeMinutes / 60.0}]] doubletime hours) worked\n"
        append output "(${minutesWorkedToday}minutes / 60minutes) * \$[format %5.2f $hp]) +  (${overtimeMinutes}minutes / 60minutes) * \$[format %6.3f [expr {$hp / 2.0}]]) + (${doubleTimeMinutes}minutes / 60minutes) * \$[format %5.2f $hp]) = \$[format %6.2f [expr {(($minutesWorkedToday / 60.0) * $hp) + (($overtimeMinutes / 60.0) * ($hp * .5)) + (($doubleTimeMinutes / 60.0) * $hp)}]]\n"
        append output "\n"

    }
    

    set grossPay \$[format %7.2f $grossPay]
    if {$details} {
        return $output
    }
}


proc ShowDetails {args} {
    destroy .details
    APSDialogBox .details \
      -name "How your salary is calculated"
    destroy .details.buttonRow.cancel.button
    APSScrolledText .st \
      -parent .details.userFrame \
      -width 150 \
      -height 50

    set output [CalculatePay -details 1]
    
    .details.userFrame.st.text insert end $output

}

ListEachDay