#!/usr/bin/perl -w use strict; my $response; # user response to querys my $HOME; # where is home my $FLEXPATH; # were we save the data my $year_suggestion; # my $t_year; # my $t_month; # my $LABEL; # my $VALUE; # my $NAME; # my $wdate; my $worked; my $start; my $lunch; my $quit; my $nominal; my $comment; my $worked_h; # my $worked_min; # my $total; # my $saldo_thismonth; my $saldo; my $total_saldo; my $old_saldo; my $old_saldo_h; my $old_saldo_min; my $saldo_h; my $saldo_min; my $saldo_thismonth_h; my $saldo_thismonth_min; my $nominal_h; my $nominal_min; my $total_h; my $total_min; my $total_saldo_h; my $total_saldo_min; my $response_ok; my $netmonth; my $netyear; my $monthtoadd; my $newsaldo; my $newsaldo_h; my $newsaldo_min; my $add; my $start_default; my $lunch_default; my $work_default; my $start_h; my $start_min; my $lunch_h; my $lunch_min; my $quit_h; my $quit_min; my $nominal_today; my $nominal_today_h; my $nominal_today_min; my $date_user; my $time_start; my $time_lunch; my $time_quit; my $time_nominal_today; my $time_worked; my $filetocount; my $time; my $nominal_day; my $time_h; my $time_min; my $file; my $create_file; my $help; my $net; my $print; my $start_time; my $lunch_time; my $work_time; my $name; my $date; my $now; my $saldo_file; my $printdate; my $mon; my $year; my $month; my $month_data; my $netmonth_suggestion; my $netmonth_suggestion_long; my $add_net; my $entry; use Getopt::Long; # flextime -- keeps track of flex time # # This keeps track of when you worked, how much you worked, and how much # you were supposed to work (as well as the difference between the two. # # It will create a series of files in the directory indicated by the # FLEXPATH variable: # # monthly data (named Nov99, Jan00, etc) # cumulative net (named saldo, duh) # # If the directory indicated by FLEXPATH does not exist the sky will # fall down on your head. Or you will be asked to fix it, whichever is # worse. # # It currently assumed that the UNIX date command exists, which perhaps # should be fixed, on general principles (TODO). # # Pär Leijonhufvud, 1999-10-23 # # Version history: # 1999-11-10: added a more flexible handeling of nominal time, as well # as som other small goodies. PKL # 1999-12-09: added support for a flex.cf file, stored at FLEXPATH # also did some work on the print section PKL # 1999-12-17: made it grab the home dir of the user form the environment # variable HOME (PKL) # 1999-12-21: added a note field to the data file and related # portions # 2000-04-20: added a suggestion of forwarding # # SET THIS TO THE DIRECTORY WHERE YOU WANT THE FILES! $HOME = $ENV{"HOME"}; # grab the HOME environment variable $FLEXPATH = "$HOME/.flex"; ### ### command line options ### &GetOptions( "help" => \$help, "net" => \$net, "print" => \$print, # make nice printout for the PHBs to look at ); ### ### If we want help ### if ($help) { print "flextime -- manages timesheets\n"; die "Syntax: flextime [--help] [--net] [--print]\n"; } ### ### Makes sure we have all we need as far and files and directories ### as well as set some general data ### # Makes sure that $FLEXPATH exists and makes sense if (! -d $FLEXPATH || ! -e $FLEXPATH || ! -X $FLEXPATH || ! -W $FLEXPATH || ! -R $FLEXPATH) { print "\n\a\aHow do you expect me to keep track of things if you\n"; print "won't give me a proper directory to store them in? \n\n"; print "Do you want me to create $FLEXPATH for you? "; chomp($response = ); if ($response eq "Y" || $response eq "y" || $response eq "yes" ) { if (! -e $FLEXPATH ) { `mkdir $FLEXPATH`; } else { die "Couldn't do it\n"; } } else { print "(Either create it with the proper permissions, or \n"; print "edit the script to point to one that's acceptable.)\n\n"; die "\$FLEXPATH ($FLEXPATH) is not acceptable, either create or change it.\n"; } } # Makes sure that condiguration file exists if (! -e "$FLEXPATH/flex.cf") { print "\n\a\aYou need to create the file $FLEXPATH/flex.cf \n"; print "It contains info on your work habits and name. \n"; print "This will become the default values, that can be changed\n"; print "in actual usage.\n\n"; print "Do you want me to help you create $FLEXPATH/flex.cf for you (y/N)? "; chomp($response = ); if ($response eq "Y" || $response eq "y" || $response eq "yes" ) { if (! -e "$FLEXPATH/flex.cf" ) { print "Please give all times in the form [h]h.mm\n"; print "\n\tWhen do you usually start working? "; chomp($start_time = ); print "\tHow long (h.mm) is your lunch? "; chomp($lunch_time = ); print "\tHow long (h.mm) is your nominal working day? "; chomp($work_time = ); print "\tWhat is your name (as you want it to appear on printouts)? "; chomp($name = ); open CONF, ">>$FLEXPATH/flex.cf"; print CONF "NAME:$name\n"; print CONF "START:$start_time\n"; print CONF "LUNCH:$lunch_time\n"; print CONF "NOMINAL:$work_time\n"; close CONF; print "\n\n"; } else { die "Couldn't do it, try manually\n"; } } else { die "\$FLEXPATH/flex.cf ($FLEXPATH/flex.cf): fix manually \n"; } } # date for file names and other uses. Will be changed # eventually to use perl internal commands (TODO) chomp($date = `date +'%Y%m%d'`); chomp($now = `date +'%H.%M'`); $saldo_file = "saldo"; chomp($printdate = `date +'%Y-%m-%d'`); ($mon,$year) = (localtime)[4,5]; $year = 1900 + $year; # make 4 digit date for Y2k compatibility # convert to 3 letter month SWITCH: { if ($mon == 0) {$month = "Jan";last SWITCH;} if ($mon == 1) {$month = "Feb";last SWITCH;} if ($mon == 2) {$month = "Mar";last SWITCH;} if ($mon == 3) {$month = "Apr";last SWITCH;} if ($mon == 4) {$month = "May";last SWITCH;} if ($mon == 5) {$month = "Jun";last SWITCH;} if ($mon == 6) {$month = "Jul";last SWITCH;} if ($mon == 7) {$month = "Aug";last SWITCH;} if ($mon == 8) {$month = "Sep";last SWITCH;} if ($mon == 9) {$month = "Oct";last SWITCH;} if ($mon == 10) {$month = "Nov";last SWITCH;} if ($mon == 11) {$month = "Dec";last SWITCH;} } # set the name of the file to work with (i.e. monthly file w. data) $month_data = "$month$year"; ### ### If we want to make a printout of (last) months timesheet ### if ($print) { # figures out a month to suggest SWITCH: { if ($mon == 0) {$netmonth_suggestion = "Dec";last SWITCH;} if ($mon == 1) {$netmonth_suggestion = "Jan";last SWITCH;} if ($mon == 2) {$netmonth_suggestion = "Feb";last SWITCH;} if ($mon == 3) {$netmonth_suggestion = "Mar";last SWITCH;} if ($mon == 4) {$netmonth_suggestion = "Apr";last SWITCH;} if ($mon == 5) {$netmonth_suggestion = "May";last SWITCH;} if ($mon == 6) {$netmonth_suggestion = "Jun";last SWITCH;} if ($mon == 7) {$netmonth_suggestion = "Jul";last SWITCH;} if ($mon == 8) {$netmonth_suggestion = "Aug";last SWITCH;} if ($mon == 9) {$netmonth_suggestion = "Sep";last SWITCH;} if ($mon == 10) {$netmonth_suggestion = "Oct";last SWITCH;} if ($mon == 11) {$netmonth_suggestion = "Nov";last SWITCH;} } # figures out a month to suggest for the long month SWITCH: { if ($mon == 0) {$netmonth_suggestion_long = "December";last SWITCH;} if ($mon == 1) {$netmonth_suggestion_long = "January";last SWITCH;} if ($mon == 2) {$netmonth_suggestion_long = "Febryary";last SWITCH;} if ($mon == 3) {$netmonth_suggestion_long = "March";last SWITCH;} if ($mon == 4) {$netmonth_suggestion_long = "April";last SWITCH;} if ($mon == 5) {$netmonth_suggestion_long = "May";last SWITCH;} if ($mon == 6) {$netmonth_suggestion_long = "June";last SWITCH;} if ($mon == 7) {$netmonth_suggestion_long = "Juli";last SWITCH;} if ($mon == 8) {$netmonth_suggestion_long = "August";last SWITCH;} if ($mon == 9) {$netmonth_suggestion_long = "Sepember";last SWITCH;} if ($mon == 10) {$netmonth_suggestion_long = "October";last SWITCH;} if ($mon == 11) {$netmonth_suggestion_long = "November";last SWITCH;} } # ask the user... print "Do you want to add to the months net to the current one \n(answer no if you have already run flextime --net) (y/N)? "; chomp($add_net = ); print "Which month do you want to print data for ($netmonth_suggestion)? "; chomp($netmonth = ); if ($netmonth eq "") { # i.e. the guess is accepted $netmonth = $netmonth_suggestion; } # fix the year since it will have changed dec-jan if ($netmonth eq "Dec" || $netmonth eq "December") { $year_suggestion = $year - 1; # last year } else { $year_suggestion = $year; # this year } # get som data out of the user on what to work on print "How do you wish to have the month and year on the printout ($netmonth_suggestion_long $year_suggestion) ? "; chomp($response = ); if ($response eq "") { $t_year = $year_suggestion; $t_month = $netmonth_suggestion_long; } else { ($t_month, $t_year) = split(/\s+/,$response); } print "Which file do you want to print data from ($netmonth_suggestion$t_year)? "; chomp($month_data = ); if ($month_data eq "") { $month_data = "$t_month$t_year"; } # A suggested file name $month_data = "$netmonth_suggestion$t_year"; #isfilethere("$FLEXPATH/$printfile"); # grab name from config file open CONF, "$FLEXPATH/flex.cf"; while () { ($LABEL, $VALUE) = split(/:/,$_); if ($LABEL eq "NAME") { # normal start time $NAME = $VALUE; chomp($NAME); } } close CONF; # remove old LaTeX files if (-e "/tmp/$month_data.tex") { print "\nDo you want to remove the old file /tmp/$month_data.tex (y/N)? "; chomp($response = ); if ($response eq "Y" || $response eq "y") { `rm -f "/tmp/$month_data.tex`; print "\n"; } else { die "\nPlease remove or rename /tmp/$month_data.tex and try again.\n\n"; } } open FILE, "$FLEXPATH/$month_data"; `cd /tmp`; open PRINTFILE, ">>/tmp/$month_data.tex"; print PRINTFILE "\\documentclass[a4paper]{article} \n"; print PRINTFILE "\\usepackage[latin1]{inputenc}\n"; print PRINTFILE "\n"; print PRINTFILE "\\pagestyle{myheadings}\n"; print PRINTFILE "\\markright{$NAME: $t_month $t_year (file: $month_data)}\n"; print PRINTFILE "\n"; print PRINTFILE "\\setlength{\\parindent}{0pt}\n"; print PRINTFILE "\\setlength{\\parskip}{1ex plus 0.5ex minus 0.2ex}\n"; print PRINTFILE "\n"; print PRINTFILE "%sections without numbers?\n"; print PRINTFILE "\\setcounter{secnumdepth}{-2}\n"; print PRINTFILE "\n"; print PRINTFILE "% how deap the TOC? 1 == section, 2 == subsection, etc\n"; print PRINTFILE "\\setcounter{tocdepth}{1}\n"; print PRINTFILE "\n"; print PRINTFILE "\\begin{document}\n"; print PRINTFILE "\n"; print PRINTFILE "\\section{Monthly timesheet for $NAME \n"; print PRINTFILE "{\\normalsize \\\\ $t_month $t_year }}\n"; print PRINTFILE "\n"; print PRINTFILE "$NAME \n"; print PRINTFILE "\n"; # make thew actual table print PRINTFILE "\\begin{tabular}{l l l l l l l}\n"; print PRINTFILE "Date & Start time & Lunch & Quit time & Worked (h) & Nominal (h) & Note \\\\ \n"; print PRINTFILE "\\hline \n"; # the data for the table foreach $entry () { ($wdate, $worked, $start, $lunch, $quit, $nominal, $comment ) = split(/\t/,$entry); ($worked_h, $worked_min) = hoursandminutes($worked); printf PRINTFILE "%-12s &%-12s &%-12s &%-12s &%2.2d:%.2d &%-12s &%25.25s \\\\ \n", $wdate, $start, $lunch, $quit, $worked_h, $worked_min, $nominal, $comment; #print "$wdate & $start & $lunch & $quit & $worked & $nominal \\\\ \n"; } # The end of the table print PRINTFILE "\\hline \n"; print PRINTFILE "\\end{tabular}\n"; print PRINTFILE "\n"; ### some data for the summary section # how much in the montly file? ($total, $nominal) = calculatesums($month_data); # this months net $saldo_thismonth = $total - $nominal; # get the current flex net open SALDO, "$FLEXPATH/$saldo_file"; if (-z "$FLEXPATH/$saldo_file") { $saldo = 0; } else { chomp($saldo = ); } close SALDO; # The total net if ($add_net eq "y" || $add_net eq "y") { $total_saldo = $saldo_thismonth + $saldo; } else { $total_saldo = $saldo; $old_saldo = $saldo - $saldo_thismonth; ($old_saldo_h, $old_saldo_min) = hoursandminutes($old_saldo); } # Get hours and minutes out, for a somewhat more readable output. ($saldo_h, $saldo_min) = hoursandminutes($saldo); ($saldo_thismonth_h, $saldo_thismonth_min) = hoursandminutes($saldo_thismonth); ($nominal_h, $nominal_min) = hoursandminutes($nominal); ($total_h, $total_min) = hoursandminutes($total); ($total_saldo_h, $total_saldo_min) = hoursandminutes($total_saldo); # Print the sums in a more or less neat table. print PRINTFILE "\n"; print PRINTFILE "\\subsection{Sums}\n"; print PRINTFILE "\n"; print PRINTFILE "\\begin{tabular}{l l }\n"; printf PRINTFILE "%-35s & %3d.%.2d \\\\ \n", "The total time worked this month:", $total_h, $total_min; printf PRINTFILE "%-35s & %3d.%2.2d \\\\ \n", "Nominal:", $nominal_h, $nominal_min; printf PRINTFILE "%-35s & %3d.%2.2d \\\\ \n", "Net this month:", $saldo_thismonth_h, $saldo_thismonth_min; if ($add_net eq "y" || $add_net eq "y") { printf PRINTFILE "%-35s & %3d.%2.2d \\\\ \n", "Net up to last month:", $saldo_h, $saldo_min; } else { printf PRINTFILE "%-35s & %3d.%2.2d \\\\ \n", "Net up to last month:", $old_saldo_h, $old_saldo_min; } #printf PRINTFILE "%-35s & %3d.%2.2d \\\\ \n", "Net up to last month:", $saldo_h, $saldo_min; printf PRINTFILE "%-35s & %3d.%2.2d \\\\ \n", "Net including this month:", $total_saldo_h, $total_saldo_min; print PRINTFILE "\\end{tabular}\n"; print PRINTFILE "\n"; print PRINTFILE "\n"; print PRINTFILE "\\vspace{2cm}\n"; print PRINTFILE "(This sheet was generated on $printdate)\n"; print PRINTFILE "\\end{document}\n"; close PRINTFILE; close FILE; # and make the dvi and ps files `latex /tmp/$month_data.tex`; #`dvips -o $FLEXPATH/$month_data.ps $FLEXPATH/$month_data.dvi`; ### ### What kind of output do we want? ### $response_ok = 0; # to test if we have an acceptable response while ($response_ok == 0) { # as long as we don't have a good response print "Do you want to create a PostScript file or a printout (postscript File/Printout)? "; chomp( $response = ); if ($response eq "F" || $response eq "f" ) { # file `dvips -o $FLEXPATH/$month_data.ps $month_data.dvi`; $response_ok = 1; } elsif ($response eq "P" || $response eq "p" ) { # printer `dvips $month_data.dvi`; $response_ok = 1; } else { # wrong response print "Wrong answer, try again\n"; } } die "done\n"; } ### ### if we just want to add the net ### if ($net) { # gets the current net out of the the file open SALDO, "$FLEXPATH/$saldo_file"; if (-z "$FLEXPATH/$saldo_file") { $saldo = 0; } else { chomp($saldo = ); } close SALDO; # figures out a month to suggest SWITCH: { if ($mon == 0) {$netmonth_suggestion = "Dec";last SWITCH;} if ($mon == 1) {$netmonth_suggestion = "Jan";last SWITCH;} if ($mon == 2) {$netmonth_suggestion = "Feb";last SWITCH;} if ($mon == 3) {$netmonth_suggestion = "Mar";last SWITCH;} if ($mon == 4) {$netmonth_suggestion = "Apr";last SWITCH;} if ($mon == 5) {$netmonth_suggestion = "May";last SWITCH;} if ($mon == 6) {$netmonth_suggestion = "Jun";last SWITCH;} if ($mon == 7) {$netmonth_suggestion = "Jul";last SWITCH;} if ($mon == 8) {$netmonth_suggestion = "Aug";last SWITCH;} if ($mon == 9) {$netmonth_suggestion = "Sep";last SWITCH;} if ($mon == 10) {$netmonth_suggestion = "Oct";last SWITCH;} if ($mon == 11) {$netmonth_suggestion = "Nov";last SWITCH;} } # get som data out of the user on what to work on print "Which month do you want to add to the net ($netmonth_suggestion)? "; chomp($netmonth = ); if ($netmonth eq "") { # i.e. the guess is accepted $netmonth = $netmonth_suggestion; } if ($netmonth eq "Dec") { $year_suggestion = $year--; } else { $year_suggestion = $year; } print "Which year ($year_suggestion)? "; chomp($netyear = ); if ($netyear eq "") { # i.e. the guess is accepted $netyear = $year_suggestion; } $monthtoadd = "$netmonth$netyear"; # name of file to evaluate isfilethere($monthtoadd); # get the sum for the file ($total, $nominal) = calculatesums($monthtoadd); # the new net $newsaldo = $saldo + $total -$nominal; # Get hours and minutes out, for a somewhat more readable output. # total ($total_h, $total_min ) = hoursandminutes($total); # net ($saldo_h, $saldo_min ) = hoursandminutes($saldo); # the new net ($newsaldo_h, $newsaldo_min ) = hoursandminutes($newsaldo); # the output print "\n"; printf "%-35s %3d:%.2d\n", "$netmonth $netyear ($monthtoadd):", $total_h, $total_min; printf "%-35s %3d:%.2d\n", "Last net:", $saldo_h, $saldo_min; printf "%-35s %3d:%.2d\n", "New net:", $newsaldo_h, $newsaldo_min; # ask to make sure printf "Do you want to update the record (Y/n)? "; chomp($response = ); if ($response eq "Y" || $response eq "y" || $response eq "") { open SALDO, ">$FLEXPATH/$saldo_file"; print SALDO $newsaldo; close SALDO; printf "Net updated (new value is %3d:%.2d).\n", $newsaldo_h, $newsaldo_min; } else { print "Net not changed.\n"; } die "\n"; # to exit the script } #($sec,$min,$hour,$mday,$mon,$year) = localtime(time); #($min,$hour,$mday,$mon,$year) = (localtime)[1,2,3,4,5]; # #$mon = $mon +1; # #$month_data = "$month$year"; #$year = 1900 + $year; # #SWITCH: { # if ($mon == 1) {$month = "Jan";last SWITCH;} # if ($mon == 2) {$month = "Feb";last SWITCH;} # if ($mon == 3) {$month = "Mar";last SWITCH;} # if ($mon == 4) {$month = "Apr";last SWITCH;} # if ($mon == 5) {$month = "May";last SWITCH;} # if ($mon == 6) {$month = "Jun";last SWITCH;} # if ($mon == 7) {$month = "Jul";last SWITCH;} # if ($mon == 8) {$month = "Aug";last SWITCH;} # if ($mon == 9) {$month = "Sep";last SWITCH;} # if ($mon == 10) {$month = "Oct";last SWITCH;} # if ($mon == 11) {$month = "Nov";last SWITCH;} # if ($mon == 12) {$month = "Dec";last SWITCH;} #} # #print "\nmin = $min, hour = $hour, mday = $mday, mon = $mon, year = $year, month = $month \n"; # ##$date = printf "%4d%2.2d%2.2d", $year, $mon, $mday; #$nynow = (printf "%2.2d.%2.2d\n",$hour, $min); #print "name = $name , now = $nynow \n"; # #die; # First we check if the expected files exists # The monthly file... isfilethere($month_data, "add"); isfilethere($saldo_file, "add"); #getopts('s:'); ### ### Now we ask what to do; either add some data, or just check what's ### already there. ### print "Add time (Y/n)? "; chomp($add = ); print "\n"; # layout improvement # If we want to add some data this is where we do that # The basic thing is to parse for sanity in the entered times # avoiding things like less than 0, more than 24 hours in a days or >60 # minutes per hour. # TODO # # 1. make it handle various input formats, such as just the hour field if # it is even, different separators, etc # # 2. Make it handle working over midnight, e.g. from 2200 to 0300 # first we grab some default values from the config file open CONF, "$FLEXPATH/flex.cf"; while () { ($LABEL, $VALUE) = split(/:/,$_); if ($LABEL eq "START") { # normal start time $start_default = $VALUE; chomp($start_default); #$start_default = s/^\s+([0-9.]+)/$1/; } elsif ( $LABEL eq "LUNCH") { # normal lunch duration $lunch_default = $VALUE; chomp($lunch_default); } elsif ( $LABEL eq "NOMINAL") { # normal workday duration $work_default = $VALUE; chomp($work_default); } } close CONF; ### If we want to add some times if ($add eq "y" || $add eq "Y" || $add eq "") { # return (blank) equals yes... # start time print "Start time ($start_default)? "; chomp($start = ); if ($start eq "" ) { $start = $start_default; } # Test for a valid time, # in format... while ($start !~ /[0-9]{1,2}.[0-9][0-9]/) { print "Start time in the format \[h\]h.mm?"; chomp($start = ); } ($start_h, $start_min) = split(/\./, $start); # ...and size. while ($start_h > 23 || $start_h < 0 || $start_min < 0 || $start_min > 59) { print "Somehow, I don't think $start is a valid time. Try again: "; chomp($start = ); ($start_h, $start_min) = split(/\./, $start); } # duration of lunch print "How long was the lunch ($lunch_default)? "; chomp($lunch = ); if ($lunch eq "") { $lunch = "$lunch_default"; } while ($lunch !~ /[0-9]{1,2}.[0-9][0-9]/) { print "Lunch time in the format \[h\]h.mm?"; chomp($lunch = ); } ($lunch_h, $lunch_min) = split(/\./, $lunch); while ($lunch_h > 23 || $lunch_h < 0 || $lunch_min < 0 || $lunch_min > 59) { print "Somehow, I don't think $lunch is a valid time. Try again: "; chomp($lunch = ); ($lunch_h, $lunch_min) = split(/\./, $lunch); } # quitting time print "Quitting time($now)? "; chomp($quit = ); if ($quit eq "") { $quit = "$now"; } while ($quit !~ /[0-9]{1,2}.[0-9][0-9]/) { print "Quit time in the format \[h\]h.mm?"; chomp($quit = ); } ($quit_h, $quit_min) = split(/\./, $quit); while ($quit_h > 23 || $quit_h < 0 || $quit_min < 0 || $quit_min > 59) { print "Somehow, I don't think $quit is a valid time. Try again: "; chomp($quit = ); ($quit_h, $quit_min) = split(/\./, $quit); } # nominal working day print "How many hours nominal ($work_default)? "; chomp($nominal_today = ); if ($nominal_today eq "") { $nominal_today = "$work_default"; } while ($nominal_today !~ /[0-9]{1,2}.[0-9][0-9]/) { print "Nominal time in the format \[h\]h.mm?"; chomp($nominal_today = ); } ($nominal_today_h, $nominal_today_min) = split(/\./, $nominal_today); while ($nominal_today_h > 23 || $nominal_today_h < 0 || $nominal_today_min < 0 || $nominal_today_min > 59) { print "Somehow, I don't think $nominal_today is a valid time. Try again: "; chomp($nominal_today = ); ($nominal_today_h, $nominal_today_min) = split(/\./, $nominal_today); } # date #printf "What date (%4d%2.2d%2.2d)? ", $year, $mon, $mday; print "What date ($date)? "; chomp($date_user= ); if ($date_user eq "") { $date_user = "$date"; } else { #while ($date_user !~ /[12][0-9]{3}[01][0-9][0123][0-9]/) { while ($date_user !~ /[12][0-9]{3}(0[1-9]|1[12])([012][0-9]|3[0-1])/) { print "Date in the format YYYYMMDD? "; chomp($date_user = ); } } # comment or note print "Note? "; chomp($comment= ); # forwarding until morning # print "Do you want to turn on forwarding of email (y/N)? "; # chomp($forwarding =); # if ($forwarding =~ /[Yy]/) { # $forwarder = `/export/home/par/bin/forwarder on`; # print "$forwarder\n"; # # ($timeoff = $start_default) =~ s/^(.*)\.(.*)$/$1$2/; # print "When do you want to turn it off again ($timeoff tomorrow)? "; # chomp($forward_off = ); # if ($forward_off eq "") { # $forward_off = "$timeoff tomorrow"; # } # open FORWARDOFF, ">$FLEXPATH/.off"; # print FORWARDOFF "/export/home/par/bin/forwarder off\n"; # close FORWARDOFF; # $atjob = `at -f $FLEXPATH/.off $forward_off\n`; # print "$atjob\n"; # unlink "$FLEXPATH/.off"; # } # figure it out $time_start = $start_h * 60 + $start_min; $time_lunch = $lunch_h * 60 + $lunch_min; $time_quit = $quit_h * 60 + $quit_min; $time_nominal_today = $nominal_today_h * 60 + $nominal_today_min; $nominal_today = ($time_nominal_today / 60); $time_worked = $time_quit -$time_start - $time_lunch; $worked = ($time_worked / 60); $worked_h = int($time_worked / 60); $worked_min = int(60 * ($worked - $worked_h)); $worked = ($time_worked / 60); if ( $worked < 0 || $worked > 24 ) { die "I really don't think it's reasonable to have worked $worked_h hours \nand some minutes. It's hours worked, not productivity.\n"; } open (OUT, ">>$FLEXPATH/$month_data") || die "Oops, can't open the file, is there something wrong?\n"; print (OUT "$date_user $worked $start $lunch $quit $nominal_today $comment\n"); #print "### $date $worked\n"; close OUT; printf "\n%-35s %3d:%2.2d %.25s\n", "You worked today:", $worked_h, $worked_min, $comment; #print "You worked $worked_h:$worked_min today.\n"; } # Now we start figuring out how much we have worked... # how much in the montly file? ($total, $nominal) = calculatesums($month_data); # this months net $saldo_thismonth = $total - $nominal; # get the current flex net open SALDO, "$FLEXPATH/$saldo_file"; if (-z "$FLEXPATH/$saldo_file") { $saldo = 0; } else { chomp($saldo = ); } close SALDO; # Get hours and minutes out, for a somewhat more readable output. ($saldo_h, $saldo_min) = hoursandminutes($saldo); ($saldo_thismonth_h, $saldo_thismonth_min) = hoursandminutes($saldo_thismonth); ($nominal_h, $nominal_min) = hoursandminutes($nominal); ($total_h, $total_min) = hoursandminutes($total); # Print the sums in a more or less neat table. printf "%-35s %3d:%.2d\n", "The total time worked this month:", $total_h, $total_min; printf "%-35s %3d:%2.2d\n", "Nominal:", $nominal_h, $nominal_min; printf "%-35s %3d:%2.2d\n", "Net this month:", $saldo_thismonth_h, $saldo_thismonth_min; printf "%-35s %3d:%2.2d\n\n", "Net up to last month:", $saldo_h, $saldo_min; sub calculatesums { $filetocount = $_[0]; # set initial values $total = 0; $nominal = 0; #print "### $filetocount \n"; ### ### For each line in the file, add upp both the time worked and the ### nominal ### # # TODO # 1. get it to store (and use) the nominal time instead of just assuming 8 # hours, which is not true a few times a year. FIXED PKL/1999-11-10 # # 2. get it to keep track of the difference between nominal and real. # FIXED PKL/1999-11-10 # # 3. there need to be a method for carrying over sums from earlier # months. FIXED # open the file for reading open FILE, "$FLEXPATH/$filetocount"; while () { #print "### $_ \n"; ($date, $time, $start, $lunch, $quit, $nominal_day) = split(/ /, $_); #print "$date\n$time,\n $start,\n $lunch,\n $quit,\n $nominal_day\n"; chomp($time); $total = $total + $time; $nominal = $nominal + $nominal_day; } close FILE; return ($total, $nominal); } sub hoursandminutes { # gets the hours and minutes out of the times $time = $_[0]; $time_h = int($time); $time_min = int(60 * ($time - $time_h)); return ($time_h, $time_min); } sub isfilethere { # make sure the files exists $file = $_[0]; if (! -e "$FLEXPATH/$file") { # and if not we try to get one to use print "I can't find one the files I need ($file).\n"; if ($_[1]) { print "Do you want to create it or supply another one (blank exits/y creates)? "; chomp($create_file = ); if ($create_file eq "") { # blank, i.e we don't want one and exits die "Ok, better luck next time.\n"; } elsif ($create_file eq "y") { # y(es) gets us to set the default `touch $FLEXPATH/$file`; print "Using $file (i.e. default).\n"; } else { # but if we get something else we use that, both for read and write if ($file eq $month_data) { print "here\n"; $month_data = $create_file; }elsif ($file eq $saldo_file) { $month_data = $create_file; } else { die "Sorry, can't figure out which one $file is.\n"; } `touch $FLEXPATH/$create_file`; print "Using $file.\n"; } } else { die "I suggest figuring out the problem and then retrying.\n"; } } return ; }