#!/usr/bin/perl # use strict; use warnings; use Locale::Currency::Format; #use Date::Calc qw( Delta_Days Today Days_in_Month ); use Date::Calc qw( Delta_Days Today Days_in_Month Add_Delta_Days); my $cnt = 0; my $amt; my $mainDir = '/Users/xxx/Scripts/BigRR/RRMarks'; my $filename = '$mainDir\test.txt'; open (my $file, '<', (@ARGV)) or die $!; open OUT, ">", $filename or die "$filename : $!\n"; # work out end of current month my ($y,$m,$d) = Today(); my $mth_end = sprintf "%04d,%02d,%02d",$y,$m,Days_in_Month($y,$m); # ($year,$month,$day) = Add_Delta_Days($year,$month,$day,$Dd); my $prev_mth_end = sprintf "%04d,%02d,%02d", Add_Delta_Days($y,$m,1,-1); printf OUT " Placement Release Tot Chg\n"; printf OUT " Date Date Days Days Cost\n"; while (<$file>) { my @dates = split '\s+', $_; $cnt++; # fill empty dates with month end my @ymd1 = split ',',$dates[4] //= $prev_mth_end; my @ymd2 = split ',',$dates[5] //= $prev_mth_end; my $diff = Delta_Days(@ymd1, @ymd2); my $free = ($diff - 3); if ($free >= 0) { my $amt = $free; my $cost = ($amt * 100); my $pfmt = "%-14s %-5s %-8s %-3s %-12s %-12s %-6s %-6s %-6s\n"; printf OUT $pfmt, $dates[0], $dates[1], $dates[2], $dates[3], fmt_mdy($dates[4]), fmt_mdy($dates[5]), $diff, $amt, fmt_curr($cost); } elsif ($free < 0) { my $amt = 0; my $cost = ($amt * 100); my $pfmt = "%-14s %-5s %-8s %-3s %-12s %-12s %-6s %-6s %-6s\n"; printf OUT $pfmt, $dates[0], $dates[1], $dates[2], $dates[3], fmt_mdy($dates[4]), fmt_mdy($dates[5]), $diff, $amt, fmt_curr($cost); } # format currency sub fmt_curr { my $num = shift; my $rounded = $num; if ($num =~ m|\.|) { $rounded = sprintf("%.2f", $num); #round to 2 decimal places } # add comma's while ($rounded =~ s/^(-?\d+)(\d\d\d)/$1,$2/) {} return ('$' .$rounded); } # change yyyy,mm,dd to mm-dd-yy sub fmt_mdy { my $ymd = shift; $ymd =~ s/ //g; # return ' 'x10 if $ymd eq $NULL_DATE; my ($y,$m,$d) = split /\D/,$ymd; if ($y < 99){ $y += 2000 }; return sprintf "%04d-%02d-%02d",$y,$m,$d; } } print $cnt, "\n";