#! /bin/perl ############################################################################# # # NOTE: This file under revision control using RCS # Any changes made without RCS will be lost # # $Source: U:\\mhansen\\bin\\RCS\\sdssum.pl,v $ # $Revision: 1.0 $ # $Date: 2003-08-03 20:59:10-04 $ # $Author: mhansen $ # $Locker: mhansen $ # $State: Exp $ # # Purpose: Utility to convert SDS monthly time file to # Semi-monthly totals, showing one line per # cost code, with one column per day, output # as a CSV file that can be opened in Excel. # see http://www.sdsdata.com/ for details on SDS. # # Directions: Usage: sdssum.pl [input_dir [output_dir]] # # input_dir expected to contain SDS tab delimited month # files with date, hours, work period, category, note, # and task name. If no input directory is specified then # hard coded default path is used. # # output_dir specifies location of output files. If no # is specified then hard coded default path is used. # # All files in the input directory starting with "Month*" # are processed. Two output files are generated for each # input file. The first, days 1-15 is prepended with # Tot1-, the second, days 16-31, is prepended with Tot2-. # # Default Location: U:\mhansen\bin # # Invoked by: user # # Copyright (C) 2003 Marc Hansen # # This program is free software; you can redistribute it and/or # modify it under the terms of version 2 of the GNU General Public # License as published by the Free Software Foundation available at: # http://http://www.gnu.org/copyleft/gpl.html # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # ############################################################################# # # # REVISION HISTORY: # # $Log: sdssum.pl,v $ # Revision 1.0 2003-08-03 20:59:10-04 mhansen # Initial revision # # # # Defaults # $INDIR = "U:/mhansen/palm/mhanse/SDS Time"; $OUTDIR = "U:/mhansen/timesheets"; # # Get directories # if ($ARGV[0]) { if (-d $ARGV[0]) { $INDIR = $ARGV[0]; } else {print "$ARGV[0] not a valid directory.\n"} } if ($ARGV[1]) { if (-d $ARGV[1]) { $OUTDIR = $ARGV[1]; } else {print "$ARGV[1] not a valid directory.\n"} } print "Input directory is: $INDIR\n"; print "Output directory is: $OUTDIR\n"; # # Main # # Build list of input files opendir(DIR, $INDIR); while (defined($file = readdir(DIR))) { if (-f "$INDIR/$file" && "$file" =~ /^Month/ ) { push @filelist, "$file"; } } closedir (DIR); # # Loop for each input file in directory foreach $file (@filelist) { print "\nProcessing $file\n"; # # Total input file into 2 hashes, one for each half of month # %month1 is 1st half, %month2 is 2nd half open IN, "<$INDIR/$file"; while () { chomp $_; # Get line ($code,$date,$hrs,$rest) = split /\t/; # Get 1st three fields $hrs =~ s/\"//g; # Drop quotes from hrs # so we can do math $date =~ /\/(\d*)\//; # Get day of month $day = $1; # Next two lines are optional # Show only first word of cost code in report # Comment out these two lines to show whole code ($a, $b) = split ' ', $code; # Get first word if ($a ne $code) {$code = $a.'"'} # Put end quote back # Add hrs to hash organized by code and day if ($day < 16) {$month1{$code}{$day} = $month1{$code}{$day} + $hrs} else {$month2{$code}{$day} = $month2{$code}{$day} + $hrs} } close IN; # # Print first half of month $outfile=$file; $outfile =~ s/^Month/Tot1-/; open OUT, ">$OUTDIR/$outfile"; print OUT "code,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15\n"; foreach $code (sort (keys %month1)) { print OUT "$code"; for ($day=1; $day<16; $day++) { if ($month1{$code}{$day}) {print OUT ",",$month1{$code}{$day} } else {print OUT '," "'} } print OUT "\n"; } close OUT; # # Print second half of month $outfile=$file; $outfile =~ s/^Month/Tot2-/; open OUT, ">$OUTDIR/$outfile"; print OUT "code,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31\n"; foreach $code (sort (keys %month2)) { print OUT "$code"; for ($day=16; $day<32; $day++) { if ($month2{$code}{$day}) {print OUT ",",$month2{$code}{$day} } else {print OUT '," "'} } print OUT "\n"; } close OUT; # Empty hashes for processing next file %month1=(); %month2=(); } __END__